如何检查是否在 Selenium 中使用 JavaScript 禁用了输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5465615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an input is disabled with JavaScript in Selenium
提问by Argote
I'm building and automated test script for a webapp using selenium and I'm trying to use the waifForConditionfunction of the API where it will wait until a JS script evaluates to true
.
我正在使用 selenium 为 webapp 构建和自动化测试脚本,我正在尝试使用API的waifForCondition函数,它会等到 JS 脚本评估为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"):
我正在尝试执行以下 JS 脚本来测试何时启用此字段(基本上是我从“谷歌搜索”中发现的):
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.
但是我得到一个SeleniumException
指示“对象不支持此属性或方法”。我能在这里做什么?任何帮助,将不胜感激。
采纳答案by Argote
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
.
我在 FireBug 控制台中运行我的问题代码并且它工作正常;但是,在执行我的脚本时,我不断收到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.
事实证明,您需要使用selenium.browserbot.getCurrentWindow()
RC 在您正在使用的主窗口而不是弹出的控制窗口上执行 JS 脚本。
As such, the JS code I actually need to evaluate ends up being this:
因此,我实际需要评估的 JS 代码最终是这样的:
selenium.browserbot.getCurrentWindow().document.getElementById('modifyHostsForm:idDnsIp0_0').disabled == false
Which works just fine. Thanks for the other hints.
哪个工作得很好。感谢其他提示。
回答by Andrew Cooper
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
回答by scunliffe
Almost... you need:
几乎......你需要:
if(document.getElementById('modifyHostsForm:idDnsIp0_0').disabled == false){
// ^- Capital "B"
//it is not disabled
}
回答by Shawn
Also make sure you are checking isDisabled == undefined
:
还要确保您正在检查isDisabled == undefined
:
isDisabled == null || isDisabled == false || isDisabled == undefined
isDisabled == null || isDisabled == false || isDisabled == undefined