How to Specify the Location of the Chromedriver Binary

How to specify the location of the chromedriver binary

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

There is a Selenium::WebDriver::Chrome.driver_path= method that allows specifying of the chromedriver binary:

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

As an alternative, you can also specify the driver path when initializing the browser. This is a bit nicer in that you do not need to have Selenium code, but would be repetitive 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 had originally answered this question, for whatever reason, I had not been able to get the above solution to work. Setting the value did not appear to be used when Selenium-WebDriver started the driver in. While the first solution is the recommended approach, this is an alternative.

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

How to specify the Chrome binary location via the selenium server standalone command line?

You can specify a non-standard location for the chrome binary in ChromeOptions.

ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

See the ChromeOptions documentation at:
https://sites.google.com/a/chromium.org/chromedriver/capabilities#TOC-Using-a-Chrome-executable-in-a-non-standard-location

Set chrome browser binary through chromedriver in Python

You can set Chrome Browser Binary location through ChromeDriver using Python ing the following different ways:


Using Options

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", )
driver.get('http://google.com/')

Using DesiredCapabilities

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities.CHROME
cap = {'binary_location': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}
driver = webdriver.Chrome(desired_capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get('http://google.com/')

Using Chrome as a Service

from selenium import webdriver
import selenium.webdriver.chrome.service as service
service = service.Service('C:\\Utility\\BrowserDrivers\\chromedriver.exe')
service.start()
capabilities = {'chrome.binary': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://www.google.com')

What is default location of ChromeDriver and for installing Chrome on Windows

For any driver that Selenium must use to open the browser (chromedriver, geckodriver, etc), you don't have to worry about where it is installed, as long as it's set in the PATH variable.

If you have it set in the OS PATH variable, you must be able to run it from the command or cmd (it's always good to make sure it's working).

Here's how you can set it (append to the existing value):

  • Article: https://www.computerhope.com/issues/ch000549.htm
  • Video: https://www.youtube.com/watch?v=dz59GsdvUF8

Then you can just instantiate it as follows:

WebDriver driver = new FirefoxDriver();

OR

WebDriver driver = new ChromeDriver();

Hope it's somehow helpful.

What is the difference in chromedriver_binary and chomedriver.exe in selenium python

chromedriver-binary

chromedriver-binary downloads and installs the chromedriver binary version 97.0.4692.36 for automated testing of webapps. The installer supports Linux, MacOS and Windows operating systems.

  • To install:

    pip install chromedriver-binary
  • Usage: To use chromedriver you need the following import:

    import chromedriver_binary

    This will add the executable to your PATH so it will be found. You can also get the absolute filename of the binary using:

    chromedriver_binary.chromedriver_filename

However with Selenium v3.x you can download the ChromeDriver and use the key executable_path to pass the absolute path of the ChromeDriver.

browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")


Conclusion

There is no best practices defined neither any efficiency matrix comparing the two approaches. It's the user perspective of comfortness. The only bonus point using executable_path is, you don't require to install any additional package.

Launch Watir/Selenium Chrome driver binary from an arbitrary location

Set Binary For Specific Browser Instance

The Chrome options can be passed from Watir to Selenium using the :desired_capabilities as "chromeOptions":

caps = {"chromeOptions" => {"binary" => 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'}}
browser = Watir::Browser.new(:chrome, desired_capabilities: caps)

Note about the binary value (from the Chromedriver page):

Path to the Chrome executable to use (on Mac OS X, this should be the
actual binary, not just the app. e.g., '/Applications/Google
Chrome.app/Contents/MacOS/Google Chrome')

Set Default Binary

Instead of setting the binary for each browser, you can also set the default binary location:

Selenium::WebDriver::Chrome.path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
browser = Watir::Browser.new :chrome


Related Topics



Leave a reply



Submit