How to check if an input is disabled with JavaScri

2019-04-23 21:49发布

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.

4条回答
Melony?
2楼-- · 2019-04-23 22:34

Almost... you need:

if(document.getElementById('modifyHostsForm:idDnsIp0_0').disabled == false){
  //                  ^- Capital "B"
  //it is not disabled
}
查看更多
Rolldiameter
3楼-- · 2019-04-23 22:43

Also make sure you are checking isDisabled == undefined:

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

查看更多
我只想做你的唯一
4楼-- · 2019-04-23 22:44

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.

查看更多
Emotional °昔
5楼-- · 2019-04-23 22:46

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
查看更多
登录 后发表回答