Ruby Watir:在JavaScript提醒上单击确定?

Mar*_*rco 9 javascript ruby ubuntu watir firewatir

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

这就是我所拥有的:

fx = FireWatir::Firefox.start(somepage)
fx.startClicker("OK")
fx.button(:id, "OK").click
fx.button(:id, "CONFIRM").click
Run Code Online (Sandbox Code Playgroud)

HTML:

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

提示中的文字可以改变,无论警报/确认提示内部是什么,我的意图是打OK.

PS:我正在运行Ubuntu.

Ali*_*ott 6

最好的方法是阻止弹出窗口触发.

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
Run Code Online (Sandbox Code Playgroud)

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