Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an input is disabled with JavaScript in Selenium

I'm building and automated test script for a webapp using selenium and I'm trying to use the waifForCondition function of the API where it will wait until a JS script evaluates to true.

I currently have this source on the page:

<input id="modifyHostsForm:idDnsIp0_0" type="text" name="modifyHostsForm:idDnsIp0_0" readonly="" disabled="">

Which should change to:

<input id="modifyHostsForm:idDnsIp0_0" type="text" name="modifyHostsForm:idDnsIp0_0">

As soon as I put a certain value on another field and fire the "blur" event on it (and this field thus becomes "enabled").

And I'm trying to execute the following JS script to test when this field is enabled (basically what I found from "Googling"):

document.getElementbyId('modifyHostsForm:idDnsIp0_0').disabled == false

However I'm getting a SeleniumException which indicates that "Object doesn't support this property or method". What can I do here? Any help would be appreciated.

like image 839
Argote Avatar asked Mar 28 '11 22:03

Argote


People also ask

How do I check if an element is disabled in selenium?

isEnabled() This method verifies if an element is enabled. If the element is enabled, it returns a true value. If not, it returns a false value.

How do you check if a button is disabled in JS?

Use the disabled property to check if an element is disabled, e.g. if (element. disabled) {} . The disabled property returns true when the element is disabled, otherwise false is returned.

How check field is editable or not in selenium?

Steps to follow: Go to “https://www.studysection.com/users/login”. Go to the “Log in” field, right-click on it and then click on inspect element. We can see the id for the login field is “UserEmail”.

How will you know whether that particular text box is disabled?

isEnabled() This will generally return true for everything but disabled input elements. Returns: True if the element is enabled, false otherwise.


1 Answers

After looking into this I found the answer. I'm documenting it here in case anyone has use for it.

I was running my question code in the FireBug console and it was working correctly; however when executing my script I kept getting SeleniumException.

It turns out that you need to use selenium.browserbot.getCurrentWindow() for the RC to execute the JS script on the main window you're using instead of the control window that pops up.

As such, the JS code I actually need to evaluate ends up being this:

selenium.browserbot.getCurrentWindow().document.getElementById('modifyHostsForm:idDnsIp0_0').disabled == false

Which works just fine. Thanks for the other hints.

like image 161
Argote Avatar answered Sep 22 '22 05:09

Argote