如何在 Selenium 中使用 JavaScript 检查输入是否被禁用
我正在使用 selenium 为 web 应用程序构建和自动化测试脚本,并且我正在尝试使用 waifForCondition 函数,它将等待直到JS 脚本的计算结果为 true
。
我目前在页面上有这个源:
<input id="modifyHostsForm:idDnsIp0_0" type="text" name="modifyHostsForm:idDnsIp0_0" readonly="" disabled="">
应该更改为:
<input id="modifyHostsForm:idDnsIp0_0" type="text" name="modifyHostsForm:idDnsIp0_0">
一旦我在另一个字段上放置了某个值并在其上触发“模糊”事件(并且该字段因此变为“启用”)。
我正在尝试执行以下 JS 脚本来测试何时启用此字段(基本上是我从“谷歌搜索”中找到的):
document.getElementbyId('modifyHostsForm:idDnsIp0_0').disabled == false
但是我收到一个 SeleniumException
,它表明“对象没有”不支持此属性或方法”。我在这里能做什么?任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
经过研究后我找到了答案。我在这里记录它,以防有人使用它。
我在 FireBug 控制台中运行我的问题代码,它工作正常;然而,当执行我的脚本时,我不断收到
SeleniumException
。事实证明,您需要使用
selenium.browserbot.getCurrentWindow()
让 RC 在您正在使用的主窗口而不是弹出的控制窗口上执行 JS 脚本。
因此,我实际需要评估的 JS 代码最终是这样的:
效果很好。感谢您的其他提示。
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:
Which works just fine. Thanks for the other hints.
尝试
您可能需要设置一个变量,然后检查它是否已设置或为空,因此:
那么条件变为:
Try
You may need to set a variable and then check if it's set or null, so:
then the condition becomes:
几乎...你需要:
Almost... you need:
还要确保您正在检查 isDisabled == undefined :
isDisabled == null || isDisabled == false || isDisabled == 未定义
Also make sure you are checking
isDisabled == undefined
:isDisabled == null || isDisabled == false || isDisabled == undefined