How to configure 'watir' to use an existing chrome user profile (created with chrome.exe --user-data-dir)

醉酒当歌 提交于 2020-05-15 04:24:05

问题


Is it possible to configure watir webdriver use an existing chrome user/profile

(created by chrome.exe --user-data-dir=C:\MyChromeUserProfile)

In Firefox it's possible to do the following:
(created a user profile with firefox -P)
profile = Selenium::WebDriver::Firefox::Profile.new(c://MyFFUserProfile) Watir::Browser.new :ff, :profile => profile

For Chrome, I tried the following code to no avail:
Watir::Browser.new :chrome, :switches => %w['--user-data-dir=c://MyChromeUserProfile']

While this opens a chrome session, it does not use the user's profile settings (Specifically an extension that was installed and configured, like Multi-pass for HTTP basic authentication).

By the way this is a similar workaround but for chrome that I am trying to implement like the one listed for Firefox and auto auth posted on http://watirwebdriver.com/basic-browser-authentication/)


回答1:


It's been a while since this question was asked but it is the top ranked Google result so I'll answer it here with the newest release of both Watir (6.0.2) and Webdriver (3.0.1). This works for me on Mac and 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


来源:https://stackoverflow.com/questions/28324552/how-to-configure-watir-to-use-an-existing-chrome-user-profile-created-with-ch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!