Ruby Watir:在 JavaScript 警报上单击“确定”?

发布于 2024-08-21 10:05:02 字数 482 浏览 3 评论 0原文

似乎我尝试过的代码都没有任何影响。我的目的是关闭所有可能通过点击“确定”按钮出现的 JavaScript 提示。问题是,我的脚本对出现的提示没有影响。换句话说,它什么也不做。

这是我所拥有的:

fx = FireWatir::Firefox.start(somepage)
fx.startClicker("OK")
fx.button(:id, "OK").click
fx.button(:id, "CONFIRM").click

HTML:

<script type="text/javascript">
    alert("Alert!");
    window.confirm("Confirm?");
</script>

提示中的文本可以更改,我的目的是无论警报/确认提示中的内容如何,​​都单击“确定”。

PS:我运行的是Ubuntu。

Seems none of the code I've tried has any affect. My intention is to close any and all JavaScript prompts that may come up by hitting the "OK" button. Problem is, my script has no affect on the prompts that come up. In other words, it does nothing.

Here's what I have:

fx = FireWatir::Firefox.start(somepage)
fx.startClicker("OK")
fx.button(:id, "OK").click
fx.button(:id, "CONFIRM").click

The HTML:

<script type="text/javascript">
    alert("Alert!");
    window.confirm("Confirm?");
</script>

The text in the prompts can change, my intention is to hit OK regardless of what is inside the alert/confirm prompt.

PS: I'm running Ubuntu.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

最单纯的乌龟 2024-08-28 10:05:02

最好的方法是完全阻止弹出窗口的触发。

require 'watir'
b = Watir::Browser.start "http://somepagewithdialogs"
# don't return anything for alert
b.execute_script("window.alert = function() {}")

# return some string for prompt to simulate user entering it
b.execute_script("window.prompt = function() {return 'my name'}")

# return null for prompt to simulate clicking Cancel
b.execute_script("window.prompt = function() {return null}")

# return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")

# return false for confirm to simulate clicking Cancel
b.execute_script("window.confirm = function() {return false}")

# interact with some element which would trigger the pop up
b.button(:id => "dialogTrigger").click

请参阅:http://watirmelon.com/2010/ 10/31/dismissing-pesky-javascript-dialogs-with-watir/ 了解更多详细信息。

The best way is to stop pop-ups from triggering at all.

require 'watir'
b = Watir::Browser.start "http://somepagewithdialogs"
# don't return anything for alert
b.execute_script("window.alert = function() {}")

# return some string for prompt to simulate user entering it
b.execute_script("window.prompt = function() {return 'my name'}")

# return null for prompt to simulate clicking Cancel
b.execute_script("window.prompt = function() {return null}")

# return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")

# return false for confirm to simulate clicking Cancel
b.execute_script("window.confirm = function() {return false}")

# interact with some element which would trigger the pop up
b.button(:id => "dialogTrigger").click

See: http://watirmelon.com/2010/10/31/dismissing-pesky-javascript-dialogs-with-watir/ for more detail.

看透却不说透 2024-08-28 10:05:02

这是很久以前就被问过的问题,所以我只是添加了一些更新的内容,为我做到了

@browser.alert.exists?
@browser.alert.ok
@browser.alert.close

第一个将返回一个布尔值
第二个就可以了,无论提示您做什么操作
第三个将关闭警报,没有

this was asked an eternity ago so I'm just adding something a little more updated that did it for me

@browser.alert.exists?
@browser.alert.ok
@browser.alert.close

first one will return a boolean
second one will ok whatever action you are prompted to do
and third one will close the alert with no

冷了相思 2024-08-28 10:05:02

弹出窗口对我来说是黑魔法。您尝试过这里的解决方案吗?

我还建议将您的问题发布到 watir-general

Pop ups are black magic to me. Did you try the solutions from here?

I would also suggest posting your question at watir-general.

沉鱼一梦 2024-08-28 10:05:02

我认为您的 fx.button(:id, "OK").click 正在等待状态更改。
但 JavaScript 对话框不会改变状态。
所以你的瓦提尔将永远等待。
如果不是那样,我不知道。

该操作不会改变状态,也不会返回状态。
所以它需要点击,无需等待。
当我使用 watir(不是 firewatir)时,@ie.button(:id, 'OK').click_no_wait。
然后最好等待 1~3 秒弹出。
然后随你喜欢。
而且如果你想控制消息框(弹出),需要AutoIT。
--这是等待消息框并单击“确定”以弹出 IE 的示例--

autoit=WIN32OLE.new('AutoItX3.Control')
autoit.WinWait('Windows Internet Explorer')
autoit.WinActive('Windows Internet Explorer')
autoit.ControlClick('Windows Internet Explorer','','OK')

我可能完全不明白您的意思。
如果是这样请忽略这一点。

I think your fx.button(:id, "OK").click was waiting state changed.
But javascript dialog does not change state.
So your watir will be waiting forever.
If not like that,I do not know.

The action will not change state, never return it.
So it needs click no wait.
When I use watir(not firewatir), @ie.button(:id, 'OK').click_no_wait.
Then better wait 1~3 second for popup.
Then as you like.
And moreover if you want control msg-box(popup), need to AutoIT.
--This is sample for wait msg-box and click ok for IE popup--

autoit=WIN32OLE.new('AutoItX3.Control')
autoit.WinWait('Windows Internet Explorer')
autoit.WinActive('Windows Internet Explorer')
autoit.ControlClick('Windows Internet Explorer','','OK')

It is possible that completely I don't understand what you mean.
If so ignore this.

ヤ经典坏疍 2024-08-28 10:05:02

查看 /var/lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html (假设这是安装 firewatir gem 的位置)。我进行了测试,它对我有用。也许阅读测试会让您深入了解 startClicker 的工作原理。

Check out /var/lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html (assuming that's where your firewatir gem is installed). I ran the test and it worked for me. Maybe reading the test will give you some insight into how startClicker is supposed to work.

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