似乎我试过的所有代码都没有任何影响。我的意图是关闭所有可能通过点击“确定”按钮出现的 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。
最佳答案
最好的方法是完全阻止弹出窗口的触发。
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/了解更多详情。
关于javascript - ruby Watir : Clicking OK on JavaScript Alerts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2249287/