通过水豚关闭 window

Close window through Capybara

我正在尝试为水豚自动化启动浏览器。浏览器正在自动填充 "Error window"(非浏览器 window)。

我需要"close/click ok"上window打开浏览器。我试图用 "AutoIT" 可执行文件处理这个弹出窗口。当我 运行 手动 AutoIT 文件时,可执行文件成功处理它。

我无法通过水豚脚本执行。代码如下

ses = Capybara::Session.new(:selenium)
IO.popen('c:\ruby\handler.exe') #- Tried this step to execute AutoIT .exe file before visit url step. 

运气不好。弹出窗口仅在引用对象时出现。

ses.execute_script "window.close()"  #- Tried this step to close the window with  the ses object. no luck in this too.

ses.visit "https://google.com"

有没有办法以编程方式关闭 window?

有几种方法可以解决这样的弹出错误:

1) 最简单的方法是执行您的方法 - 在调用水豚访问导致错误弹出的站点之前执行自动脚本。您应该确保您正在执行的 autoit 脚本在尝试关闭它之前等待 window 出现(参见:https://www.autoitscript.com/autoit3/docs/functions/WinWait.htm 以供参考)。

2) 您可以执行另一个 ruby script/thread(请记住 ruby 中的线程有点复杂),它会在后台执行 autoit 脚本(在循环中) 并等待成功响应。

3) 您可以尝试禁用导致浏览器中弹出错误的任何内容。

如果您仍然无法解决问题,我将能够在几个小时内提供一些遵循方法 1 和 2 的代码。

您似乎可以使用 Chrome 浏览器 注册自定义 selenium 驱动程序 并指定命令行选项以禁用所有扩展,使用以下驱动程序注册表代码:

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome, 
                                      :switches => %w[--disable-extensions])
end

ses = Capybara::Session.new(:selenium)
...

我使用信息 here and the list of available command line options here 将其组合在一起(虽然我自己没有测试过)。

错误 Failed to load Extension. Loading of unpacked extensions are disabled by administrator. 表示您的系统设置为禁用带有 Chrome 的扩展。因此,即使您设法关闭它,您也可能无法使用 Selenium 实现 Chrome 自动化,因为它需要 Chrome 以驱动程序作为扩展来启动。

让它发挥作用的最佳机会可能是禁用限制或将扩展程序添加到白名单。

这里是关于这个问题的link: https://bugs.chromium.org/p/chromedriver/issues/detail?id=639