How to specify the location of a binary code of a chrome rib

I used to put the Chrome binary “chromedriver.exe” in the “C: / Windows” directory, and Batyr collected it from there. Now I need to move my project to another machine so that I cannot hardcode the executable path. I also want the binary file to be stored with our Git code, rather than forcing each test engineer to manually update the binary file as newer versions are released.

Now I installed the Chrome binary in the absolute path, but it was not found. Here is what I tried (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 

Output:

 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' 

I am on Windows 7 using Ruby version 1.9.3p551, and I mean the tutorial http://watirwebdriver.com/chrome/ .

How to tell Watir (and Selenium-WebDriver) the location of the chromedriver.exe file?

+7
ruby google-chrome watir
source share
2 answers

Solution 1 - Selenium :: WebDriver :: Chrome.driver_path =

There is a method Selenium::WebDriver::Chrome.driver_path= , which allows you to specify the binary code of the chrome edge:

 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 

Solution 2 - Specify: driver_path_ during browser initialization

Alternatively, you can also specify the path to the driver when initializing the browser. This is slightly better in that you do not need to have Selenium code, but will repeat if you initialize the browser in different places.

 # 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 

Solution 3 - Update ENV ['PATH']

When I initially answered this question, for some reason, I was not able to get the above solution to work. The value setting was apparently not used when Selenium-WebDriver started the driver. Although the first solution is recommended, it is an alternative.

Another option is to programmatically add the desired directory to the path that is stored in ENV['PATH'] . You can see in Selenium :: WebDriver :: Platform that the binary is located, checking if the executable exists in any of the folders in the path (starting from version 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 

To specify the folder containing the binary, you just need to change ENV['PATH'] (add directory):

 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 
+10
source share

In Selenium webdriver 3.x configs, change:

 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: Determine the path to the chrome transfer chrome page

binary: Define the path to the binary chrome app chromepage . You can use the chrome portable to use different versions of chrome.

+2
source share

All Articles