Java Selenium Webdriver - 验证文本框写保护?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9768622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:26:23  来源:igfitidea点击:

Selenium Webdriver - verify text box write-protected?

javaselenium-webdriverwebdriver

提问by ABCDEFG

How can we verify whether a field is write-protected (that is, readonly) in Selenium using Java code?

我们如何使用 Java 代码验证 Selenium 中的字段是否被写保护(即只读)?

Best regards

此致

采纳答案by Pavel Zorin

You can try to write something via sendkeys() and check that value attribute of textbox has not been changed.

您可以尝试通过 sendkeys() 编写一些内容并检查文本框的 value 属性是否未更改。

回答by Ken Brittain

The WebElementinterface has a function called isEnabled.

WebElement接口有一个名为isEnabled的函数。

回答by Tarken

Since I don't know for what you need that check i'll post some examples that might be usefull.

由于我不知道您需要什么检查,我将发布一些可能有用的示例。

driver.findElements(By.cssSelector("input:not([readonly='readonly'])[type='text']"));

=> returns all text input fields that are editable

=> 返回所有可编辑的文本输入字段

WebElement element = driver.findElement(By.id("username");
//can fail if the attribute is not there
String attribute  = element.getAttribute("readonly"); 

=> might need a try catch block

=> 可能需要一个 try catch 块

回答by nu1silva

with selenium-java 2.21.0 you can check if its enabled

使用 selenium-java 2.21.0,您可以检查它是否已启用

driver.findElement(By.id("...")).isEnabled()

回答by Anatoliy

  1. isEnabled()does not have any common things to readonly.
  2. String attribute = element.getAttribute("readonly");will not fail your test even "readonly" is absent. In this case it returns null, but we need exception.
  1. isEnabled()没有任何常见的只读内容。
  2. String attribute = element.getAttribute("只读"); 即使不存在“只读”,也不会使您的测试失败。在这种情况下,它返回null,但我们需要异常。

Use like this:

像这样使用:

    WebElement some_element = driver.findElement(By.id("some_id"));
    String readonly = some_element.getAttribute("readonly");
    Assert.assertNotNull(readonly);

Do NOT verify getAttribute("readonly").equals("true")or similar, in different browsers it can be different as well. (readonly="readonly" in IE, readonly="" in FF, etc.)

不要验证getAttribute("readonly").equals("true")或类似的,在不同的浏览器中它也可能不同。(在 IE 中为 readonly="readonly",在 FF 中为 readonly="" 等)

回答by Princess S

Below is the Code works best for me. Hope this will be usefull

以下是最适合我的代码。希望这会很有用

    WebElement fieldName = driver.findElement(By.id("enter_id"));
    fieldName.sendKeys("abc");
    String fieldNameVal = fieldName.getAttribute("value");
    if(fieldNameVal.contentEquals("abc")){
    System.out.println("Field is editable");
    }
    else{
    System.out.println("Field is non editable" + fieldNameVal);
    }

回答by yaobin

This is a late answer but because I'm working on this recently, I'd like to share my solution.

这是一个迟到的答案,但因为我最近正在研究这个,我想分享我的解决方案。

I implemented a method to test if a text box is read-only or not. I'm working on a Django project so the code is in Python, but I believe it's easy to port it to Java:

我实现了一种方法来测试文本框是否为只读。我正在处理一个 Django 项目,所以代码是用 Python 编写的,但我相信将它移植到 Java 很容易:

from django.contrib.staticfiles.testing import StaticLiveServerTestCase

class MyTest(StaticLiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()

    def _is_text_box_read_only(self, web_element):
        old_text = web_element.get_attribute("value")
        web_element.send_keys("test")
        new_text = web_element.get_attribute("value")
        web_element.clear()
        web_element.send_keys(old_text)  # Resume to the previous text
        return old_text == new_text

Note that:

注意:

  1. You need to use the attribute valueto get the entered text.
  2. Better to resume to the previous text after the testing.
  3. The code is just an excerpt that shows the core ideas. It may not be runnable.
  1. 您需要使用该属性value来获取输入的文本。
  2. 测试后最好恢复到之前的文本。
  3. 代码只是显示核心思想的摘录。它可能无法运行。

回答by Fahim Khatri

I know it's been answered. but unfortunately, when I facing a similar issue, I had to spend some time to find the correct solution.
I agree that .getAttribute("readonly") returns null but to handle it > I wrote something similar to this:

我知道已经回答了。但不幸的是,当我遇到类似的问题时,我不得不花一些时间来找到正确的解决方案。
我同意 .getAttribute("readonly") 返回 null 但要处理它> 我写了类似的东西:

public boolean isreadOnly(element) {        
    Boolean readOnly = false;       
    readOnly = ((element.getAttribute("disabled") != null) || (element.getAttribute("readonly") != null));  
    return readOnly;        
}

and call above:

并在上面调用:

WebElement text = driver.findElement("locator"));  
return isReadOnly(text);

回答by JohnP2

Here is yet another answer, using javascript

这是另一个答案,使用 javascript

Boolean isEditable = js.executeScript("if(arguments[0].nodeName==='INPUT' || arguments[0].nodeName==='TEXTAREA'){ return !arguments[0].disabled && !arguments[0].readOnly; }else{ return arguments[0].isContentEditable};", webelement);

This answer is based on https://stackoverflow.com/a/28269518and is simplified a bit. In regular Javascript, it would look like this:

这个答案基于https://stackoverflow.com/a/28269518并简化了一点。在常规 Javascript 中,它看起来像这样:

function isEditable(element){
    if(element.nodeName==="INPUT" || element.nodeName==="TEXTAREA"){
        return !element.disabled && !element.readOnly;
    }else{
        return element.isContentEditable;
    }
}

This addresses both the "readOnly" and the "disabled" properties, and it can be used on any type of element.

这解决了“只读”和“禁用”属性,并且它可以用于任何类型的元素。