如何指定chromedriver二进制文件的位置

时间:2014-12-31 09:55:11

标签: ruby google-chrome watir

之前我已将Chrome二进制文件“chromedriver.exe”放在“C:/ Windows”目录中,而Watir则从那里选择它。现在我必须将我的项目移动到另一台机器,因此我无法对可执行路径进行硬编码。我还希望二进制文件与Git上的代码保持一致,而不是让每个测试工程师在发布新版本时手动更新二进制文件。

现在我已将Chrome二进制文件放在绝对路径上,但未找到它。这是我尝试过的(hooks.rb):

  Before do
    puts "inside hooks in before"
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile['download.prompt_for_download'] = false
    profile['download.default_directory'] = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers/chromedriver.exe")
    @browser = Watir::Browser.new :chrome, :profile => profile
  end

输出结果为:

inside hooks in before

Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver.
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/service.rb:21:in `executable_path'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/service.rb:34:in `default_service'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/bridge.rb:14:in `initialize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/common/driver.rb:37:in `new'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/common/driver.rb:37:in `for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver.rb:67:in `for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/browser.rb:46:in `initialize'
C:/Users/Admin/watircukepractice/test_puppies/features/support/hooks.rb:11:in `new'
C:/Users/Admin/watircukepractice/test_puppies/features/support/hooks.rb:11:in `Before'

我在Windows 7上,使用Ruby版本1.9.3p551,我指的是教程http://watirwebdriver.com/chrome/

如何告诉Watir(和Selenium-WebDriver)chromedriver.exe的位置?

3 个答案:

答案 0 :(得分:12)

解决方案1 ​​ - Selenium :: WebDriver :: Chrome.driver_path =

Selenium::WebDriver::Chrome.driver_path=方法允许指定chromedriver二进制文件:

require 'watir'

# Specify the driver path
chromedriver_path = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers","chromedriver.exe")
Selenium::WebDriver::Chrome.driver_path = chromedriver_path

# Start the browser as normal
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.close

解决方案2 - 在浏览器初始化期间指定:driver_path

作为替代方法,您还可以在初始化浏览器时指定驱动程序路径。这样做更好一点,因为您不需要使用Selenium代码,但如果您在不同的地方初始化浏览器,则会重复。

# Determine the driver path
chromedriver_path = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers","chromedriver.exe")

# Initialize the browser with the driver path
browser = Watir::Browser.new :chrome, driver_path: chromedriver_path

解决方案3 - 更新ENV ['PATH']

当我最初回答这个问题时,无论出于何种原因,我都无法得到上述解决方案。当Selenium-WebDriver启动驱动程序时,似乎没有使用设置值。虽然第一种解决方案是推荐的方法,但这是一种替代方案。

另一种选择是以编程方式将所需目录添加到路径中,该路径存储在ENV['PATH']中。你可以在Selenium :: WebDriver :: Platform中看到二进制文件是通过检查路径中的任何文件夹(版本2.44.0)中是否存在可执行文件而找到的:

def find_binary(*binary_names)
  paths = ENV['PATH'].split(File::PATH_SEPARATOR)
  binary_names.map! { |n| "#{n}.exe" } if windows?

  binary_names.each do |binary_name|
    paths.each do |path|
      exe = File.join(path, binary_name)
      return exe if File.executable?(exe)
    end
  end

  nil
end

要指定包含二进制文件的文件夹,只需更改ENV['PATH'](附加目录):

require 'watir'

# Determine the directory containing chromedriver.exe
chromedriver_directory = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers")

# Add that directory to the path
ENV['PATH'] = "#{ENV['PATH']}#{File::PATH_SEPARATOR}#{chromedriver_directory}"

# Start the browser as normal
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.close

答案 1 :(得分:2)

Selenium webdriver 3.x配置中,更改:

caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"binary" => <path to chrome (example: chrome portable)>})
Capybara::Selenium::Driver.new(app, :browser => :chrome, :driver_path => <path to chrome driver>, :desired_capabilities => caps)

driver_path:定义chromedriver的路径chromedriver page

二进制:定义二进制Chrome应用chromepage的路径。您可以使用chrome portable来使用不同版本的chrome。

答案 2 :(得分:0)

直接更新您的硒driver_path

只需在启动新的Chrome浏览器窗口之前调用此方法:

Selenium::WebDriver::Chrome::Service.driver_path = Rails.root.join( "lib", "chromedriver" ).to_s

当然,请更改您的chromedriver所在的路径。

注意:driver_path必须是字符串,因此请勿传入FilePath对象。