如何通过Ruby Selenium Binding(或WATIR)创建chrome配置文件

时间:2018-07-05 13:28:53

标签: ruby selenium selenium-webdriver watir

我知道如何为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配置文件吗?

3 个答案:

答案 0 :(得分:1)

使用现有或创建新的配置文件可以通过Chromedrivers user-data-dir参数来完成。在Watir中,您可以通过CREATE TABLE analysis ( id BIGSERIAL NOT NULL, offer1 numeric(19, 2) NOT NULL, roi1 numeric(19, 2) NOT NULL, offer2 numeric(19, 2) NOT NULL, roi2 numeric(19, 2) NOT NULL, offer3 numeric(19, 2) NOT NULL, roi3 numeric(19, 2) NOT NULL, PRIMARY KEY (id)); 参数传递参数:

:args

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

答案 1 :(得分:0)

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

 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

答案 2 :(得分:0)

由于您在评论中要求提供有关水豚的更详细的说明,因此我将其发布为答案(尽管您现在似乎已经有了可行的解决方案-抱歉,回答延迟)。

在我的rails项目中,我通常按以下方式配置Selenium chrome驱动程序:

<attributeList>
    <attribute id="1680231134">
        <attributeCode>LastName</attributeCode>
        <attributeValue>Patil</attributeValue>
    </attribute>
</attributeList>
Gemfile中的

(或在本地安装)。然后在系统测试的初始化程序中定义

gem 'chromedriver-helper'

及更高版本(配置RSpec),将其设置为使用的驱动程序,例如:

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

也许这可以帮助某人。干杯

编辑:添加了chromedriver-helper