【问题标题】:How to create chrome profile via Ruby Selenium Binding(or WATIR)如何通过 Ruby Selenium Binding(或 WATIR)创建 chrome 配置文件
【发布时间】:2018-12-14 00:17:07
【问题描述】:

我知道如何为 Firefox 创建配置文件

require 'watir'
options = Selenium::WebDriver::Firefox::Options.new
options.profile = "default"
@driver = Selenium::WebDriver.for :firefox, options: options
@b = Watir::Browser.new @driver

但是当我为 Chrome 做同样的事情时它并没有创建,事实上我意识到选项(请看上面)对象甚至没有方法 profile= 所以我尝试添加如下所示的配置文件(我看到人们如何在 Java Selenium Binding 中创建,所以我做了同样的事情,但在这里不起作用)

options = Selenium::WebDriver::Firefox::Options.new
options.add_argument('user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default')
@driver = Selenium::WebDriver.for :chrome, options: options

有人可以帮助我如何通过 Ruby Selenium 绑定(或 WATIR)创建 Chrome 配置文件吗?

【问题讨论】:

  • 我不确定 RUBY ,但你是为 JAVA 还是 Python 开放?
  • @cruisepandey 我知道用 Java 做,我不知道如何用 Ruby 做,这就是问题所在。
  • 我不确定什么适用于 Watir,但是在设置 Capybara(另一个用于自动化 gui 测试的框架)时,我们需要将 browser_options(::Selenium::WebDriver::Chrome::Options.new 类型)设置为 --headless --disable-gpu --no-sandbox。也许这有帮助
  • @JaySchneider 我知道水豚,你能写完整的代码吗?

标签: ruby selenium selenium-webdriver watir


【解决方案1】:

可以通过 Chromedrivers user-data-dir 参数来使用现有配置文件或创建新配置文件。在 Watir 中,您可以通过 :args 参数传递参数:

browser = Watir::Browser.new :chrome, 
  args: ['user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data']

请注意,如果您尝试使用现有的默认配置文件,您不希望在路径中包含“默认”目录。

【讨论】:

  • 像魅力一样工作!谢谢贾斯汀!
【解决方案2】:

我做了一个创建新浏览器的功能。你可以使用它。

 def new_browser
  if Rails.env.production?
    chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
    Selenium::WebDriver::Chrome.path = "/app/.apt/usr/bin/google-chrome"
    Selenium::WebDriver::Chrome.driver_path = "/app/vendor/bundle/bin/chromedriver"
  end


  profile = Selenium::WebDriver::Chrome::Profile.new
  profile['general.useragent.override'] = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'

  driver = Selenium::WebDriver.for :chrome, :profile => profile
  pid = driver.instance_variable_get(:@service).instance_variable_get(:@process).instance_variable_get(:@pid)

  begin
    browser = Watir::Browser.new driver
  rescue => e
    system("kill -9 #{@pid}")
  end
  return {:browser => browser , :pid => pid}
end

【讨论】:

  • 这是什么?您在哪里设置 chrome 配置文件?路径在哪里?这对我不起作用,您的代码没有设置个人资料。
  • 我认为这是 chrome 配置文件 {profile = Selenium::WebDriver::Chrome::Profile.new profile['general.useragent.override'] = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'} 然后这个配置文件用在 webdriver 中。
【解决方案3】:

由于您在 cmets 中要求对 Capybara 进行更详细的解释,因此我将其发布为答案(尽管您现在似乎已经有了一个可行的解决方案 - 抱歉延迟回答)。

在我的 Rails 项目中,我通常如下配置 Selenium chrome 驱动程序:

gem 'chromedriver-helper'

在 Gemfile 中(或在本地安装)。然后在系统测试初始化​​器中定义

Capybara.register_driver :selenium_chrome_headless_no_sandbox do |app|
  browser_options = ::Selenium::WebDriver::Chrome::Options.new
  browser_options.args << '--headless'
  browser_options.args << '--disable-gpu'
  browser_options.args << '--no-sandbox'
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end

后来(配置 RSpec)我将它设置为使用的驱动程序,如:

RSpec.configure do |config|
  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome_headless_no_sandbox
  end
end

也许这对某人有帮助。干杯

编辑:添加 chromedriver-helper

【讨论】:

  • 谢谢,但我一直想知道它是如何工作的?你能看看 JustinKo 的答案,他在哪里包含个人资料的目录路径!
  • 我忘了提到我使用的是 chromedriver-helper gem。我编辑了答案。如果这对你有用,不妨试试?
猜你喜欢
  • 2020-05-19
  • 2018-12-18
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
相关资源
最近更新 更多