最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find input field is readonly or not in using selenium webdriver with java - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Dec 1, 2020 at 11:05 AndyUK 4,0037 gold badges45 silver badges47 bronze badges asked Jul 7, 2014 at 11:16 Karim NarsindaniKarim Narsindani 4544 gold badges14 silver badges38 bronze badges 1
  • share the HTML snippet of the disabled field, before and after clicking on button. – HemaSundar Commented Jul 7, 2014 at 13:15
Add a comment  | 

5 Answers 5

Reset to default 8

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....

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

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. :-)

<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.

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);
发布评论

评论列表(0)

  1. 暂无评论