4

是否可以使用现有的 chrome 用户/配置文件配置 watir webdriver

由 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 基本身份验证的 Multi-pass)。

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

4

1 回答 1

1

自从提出这个问题以来已经有一段时间了,但它是谷歌排名最高的结果,所以我将在这里用 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
于 2016-11-16T01:47:58.133 回答