简体   繁体   中英

How to find input field is readonly or not in using selenium webdriver with java

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?

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:

   WebElement readOnly = driver.findElementBy(locator);
   Assert.assertTrue(readOnly.getAttribute("readOnly").equals("true"),"Element ReadOnly")

Hope this helps....

Using javascript, you can get this attribute value like -

var isReadOnly = document.getElementById("field").readOnly;
alert(isReadOnly); //displays true OR false

Please check browser compatibility for this attribute. :-)

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.

System.out.println(driver.findElement(By.xpath(txt_Username)).getAttribute("disabled"));

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);
<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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM