【问题标题】:Ruby Watir: Clicking OK on JavaScript Alerts?Ruby Watir:在 JavaScript 警报上单击“确定”?
【发布时间】:2011-01-15 23:56:12
【问题描述】:

似乎我尝试过的所有代码都没有任何影响。我的意图是关闭所有可能通过点击“确定”按钮出现的 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。

【问题讨论】:

    标签: javascript ruby ubuntu watir firewatir


    【解决方案1】:

    这是很久以前被问到的,所以我只是添加了一些更新的东西,为我做了这件事

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

    第一个将返回一个布尔值 第二个将确定您被提示执行的任何操作 第三个将关闭警报,没有

    【讨论】:

      【解决方案2】:

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

      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/。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

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

          我可能完全不明白你的意思。 如果是这样,请忽略它。

          【讨论】:

            【解决方案5】:

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

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

            【讨论】:

            • 使用解决方案 #3 在上面的 Željko 第二个链接中为我工作。我稍微修改了 startClicker 方法以获取浏览器变量参数,但这是最终起作用的方法。
            猜你喜欢
            • 2013-11-15
            • 1970-01-01
            • 2021-04-19
            • 2011-11-08
            • 2013-08-29
            • 1970-01-01
            • 1970-01-01
            • 2016-12-26
            • 2012-05-29
            相关资源
            最近更新 更多