如何将'watir'配置为使用现有的 chrome 用户配置文件(使用 chrome 创建.exe --user-data-dir)



是否可以使用现有的chrome用户/配置文件配置watir Web驱动程序

由chrome创建.exe --user-data-dir=C:\MyChromeUserProfile

Firefox 中,可以执行以下操作:
使用 Firefox -P 创建用户配置文件
profile = Selenium::WebDriver::Firefox::Profile.new(c://MyFFUserProfile) Watir::Browser.new :ff, :profile => profile

对于Chrome,我尝试了以下代码无济于事:
Watir::Browser.new :chrome, :switches => %w['--user-data-dir=c://MyChromeUserProfile']

虽然这会打开 chrome 会话,但它不会使用用户的配置文件设置(特别是已安装和配置的扩展程序,例如用于 HTTP 基本身份验证的多通道)。

顺便说一下,这是一个类似的解决方法,但对于我正在尝试实现的 chrome,例如 http://watirwebdriver.com/basic-browser-authentication/) 上发布的 Firefox 和自动身份验证列出的解决方法

这个问题问已经有一段时间了,但它是排名最高的谷歌结果,所以我将在这里用最新版本的Watir (6.0.2)和Webdriver(3.0.1)来回答它。这在Mac和Windows上对我有用:

require 'watir'
switches = %W[--user-data-dir=c:\chrome]
# switches = %W[--user-data-dir=/chrome]  => Linux or Mac

prefs = {
  :download => {
    :prompt_for_download => false,
    :default_directory => "c:\downloads"
  }
}
browser = Watir::Browser.new :chrome, :prefs => prefs, switches: switches
browser.goto 'google.com'
browser.text_field(title: 'Search').set 'Hello World!'
browser.button(type: 'submit').click
sleep 10
puts browser.title
browser.close

最新更新