admin管理员组

文章数量:1201982

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.

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.

Share Improve this question edited Apr 10, 2012 at 15:19 Argote asked Mar 28, 2011 at 22:04 ArgoteArgote 2,1551 gold badge15 silver badges20 bronze badges 3
  • p.s. checking for the readonly attribute would also work – Argote Commented Mar 28, 2011 at 22:07
  • are you using double equals to assign in your code? disabled == false ?? – amosrivera Commented Mar 28, 2011 at 22:12
  • 1 no, it is not an assignment, it is a check. – Argote Commented Mar 28, 2011 at 22:15
Add a comment  | 

4 Answers 4

Reset to default 7

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.

Try

document.getElementById('modifyHostsForm:idDnsIp0_0').getAttribute('disabled') == false

You may need to set a variable and then check if it's set or null, so:

var isDisabled = document.getElementById('modifyHostsForm:idDnsIp0_0').getAttribute('disabled')

then the condition becomes:

isDisabled == null || isDisabled == false

Almost... you need:

if(document.getElementById('modifyHostsForm:idDnsIp0_0').disabled == false){
  //                  ^- Capital "B"
  //it is not disabled
}

Also make sure you are checking isDisabled == undefined:

isDisabled == null || isDisabled == false || isDisabled == undefined

本文标签: How to check if an input is disabled with JavaScript in SeleniumStack Overflow