如何在 Selenium 中使用 JavaScript 检查输入是否被禁用

发布于 2024-10-27 15:38:33 字数 908 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

零度℉ 2024-11-03 15:38:33

经过研究后我找到了答案。我在这里记录它,以防有人使用它。

我在 FireBug 控制台中运行我的问题代码,它工作正常;然而,当执行我的脚本时,我不断收到SeleniumException

事实证明,您需要使用 selenium.browserbot.getCurrentWindow()让 RC 在您正在使用的主窗口而不是弹出的控制窗口上执行 JS 脚本。

因此,我实际需要评估的 JS 代码最终是这样的:

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

效果很好。感谢您的其他提示。

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.

无需解释 2024-11-03 15:38:33

尝试

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

您可能需要设置一个变量,然后检查它是否已设置或为空,因此:

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

那么条件变为:

isDisabled == null || isDisabled == false

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
掩于岁月 2024-11-03 15:38:33

几乎...你需要:

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

Almost... you need:

if(document.getElementById('modifyHostsForm:idDnsIp0_0').disabled == false){
  //                  ^- Capital "B"
  //it is not disabled
}
酒绊 2024-11-03 15:38:33

还要确保您正在检查 isDisabled == undefined :

isDisabled == null || isDisabled == false || isDisabled == 未定义

Also make sure you are checking isDisabled == undefined:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文