使用带有java的selenium webdriver时如何查找输入字段是否为只读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24609327/
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
How to find input field is readonly or not in useing selenium webdriver with java
提问by Karim Narsindani
I am using selenium webdriver with java to write the script. However, we have few fields which are getting disabled after click on button. We need to find this field are getting readonly mode or not. I have use isEnabled
and isDisplayed
but it is not working, the system displays NoSuchElementFound
expection. Is there any way to handle this?
我正在使用带有 java 的 selenium webdriver 来编写脚本。但是,我们有几个字段在单击按钮后被禁用。我们需要找到这个字段是否处于只读模式。我用过isEnabled
,isDisplayed
但不工作,系统显示NoSuchElementFound
预期。有没有办法处理这个?
采纳答案by Zach
You can achieve this without having to execute JS, all you need to do is to get the DOM Attribute of the element which is "ReadOnly" this can be achieved using:
您可以在无需执行 JS 的情况下实现这一点,您需要做的就是获取“只读”元素的 DOM 属性,这可以使用以下方法实现:
WebElement readOnly = driver.findElementBy(locator);
Assert.assertTrue(readOnly.getAttribute("readOnly").equals("true"),"Element ReadOnly")
Hope this helps....
希望这可以帮助....
回答by Ved
Using javascript, you can get this attribute value like -
使用 javascript,您可以获得此属性值,例如 -
var isReadOnly = document.getElementById("field").readOnly;
alert(isReadOnly); //displays true OR false
Please check browser compatibility for this attribute. :-)
请检查此属性的浏览器兼容性。:-)
回答by Karim Narsindani
This code is working with my application:
此代码适用于我的应用程序:
public boolean runScript(String str){
String script ="if($("+'"'+"#City"+'"'+").attr("+'"'+str+'"'+")){return true}else{return false}";
System.out.println(script);
JavascriptExecutor js = (JavascriptExecutor) driver;
return (Boolean) js.executeScript(script);
}
int k=0;
do{
if(!runScript("HouseNo")){
System.out.println("Field is in readonly mode");
break;
}
Thread.sleep(1000);
}while(k<60);
回答by SENoob
You can do the following steps. 1. Use the WebDriverWait wait statement to make sure that the element is indeed present before you are doing the check. 2. Go through the HTML script and check what attribute is causing the HTML element to be read only. In the application that I use the attribute is'disabled'. When the value of the attribute 'disabled' was TRUE the element was disabled. For you it might be a different attribute. Then fetch the value of this attribute and you will find out if the element is enabled or disabled.
您可以执行以下步骤。1. 在进行检查之前,使用 WebDriverWait 等待语句确保该元素确实存在。2. 浏览 HTML 脚本并检查是什么属性导致 HTML 元素为只读。在我使用该属性的应用程序中,“已禁用”。当属性 'disabled' 的值为 TRUE 时,元素被禁用。对你来说,它可能是一个不同的属性。然后获取该属性的值,您将发现该元素是启用还是禁用。
System.out.println(driver.findElement(By.xpath(txt_Username)).getAttribute("disabled"));
System.out.println(driver.findElement(By.xpath(txt_Username)).getAttribute("disabled"));
回答by Zv Lfdl
<div class="col-sm-8 col-md-9 currency">
<input id="test" type="<some type>" value="777.00" class="read-only" readonly="true">
<input id="some_input_id_here" type="text" value="<some string value here>" name="<some name here>" class="read-only" readonly="true">
</div>
Lets use the above as example, which i have a form and i'm checking the elements using Chrome's DevTools (or any other preferred tool). Say i've got the css selector for the disabled fields that contains '777.00' in value and the element type 'css selector' is: #sampleForm > fieldset > div:nth-child(1) > div.col-sm-5.col-md-6 > input
让我们以上面为例,我有一个表单,我正在使用 Chrome 的 DevTools(或任何其他首选工具)检查元素。假设我有禁用字段的 css 选择器,其值包含“777.00”,元素类型“ css 选择器”是:#sampleForm > fieldset > div:nth-child(1) > div.col-sm-5 .col-md-6 > 输入
Then my code to check on this element being read-only is:
然后我检查这个元素是否只读的代码是:
//i have stored my elements somehwere on a file as String
String disElement = "#sampleForm > fieldset > div:nth-child(1) > div.col-sm-5.col-md-6 > input"
assertTrue(WebDriver.findElement(By.cssSelector(disElement)).getAttribute("class").equalsIgnoreCase("read-only"));
Note that i'm accessing the element's class layer and verify that it is "read-only" using TestNG assertTrue after getting the element's attribute of "class". The asserTrue boolean will return false if the element is not read only. Also check the layers of element you're accessing whether or not it is the parent, child, span, etc. Hope this can be of any help as well. thanks.
请注意,我正在访问元素的类层,并在获取元素的“类”属性后使用 TestNG assertTrue 验证它是“只读”的。如果元素不是只读的,asserTrue 布尔值将返回 false。还要检查您正在访问的元素层是否是父元素、子元素、跨度等。希望这也能有所帮助。谢谢。