How to specify the location of the chromedriver binary

前端 未结 3 1845
时光取名叫无心
时光取名叫无心 2020-12-16 17:38

Earlier I had put the Chrome binary, \"chromedriver.exe\", in the \"C:/Windows\" directory and Watir was picking it from there. Now I have to move my project to another mach

相关标签:
3条回答
  • 2020-12-16 18:05

    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
    
    0 讨论(0)
  • 2020-12-16 18:05

    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: Define path to chromedriver chromedriver page

    binary: Define path to binary chrome app chromepage. You can use a chrome portable to use differents versions of chrome.

    0 讨论(0)
  • 2020-12-16 18:14

    Update your Selenium driver_path directly.

    Just call this before launching your new Chrome browser window:

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

    Of course, change the path to where your chromedriver is.

    NOTE: driver_path needs to be a String, so don't pass in a File or Path object.

    0 讨论(0)
提交回复
热议问题