如何指定chromedriver二进制文件的位置 [英] How to specify the location of the chromedriver binary
问题描述
现在我已将Chrome二进制文件在绝对路径上,但它没有被发现。这是我试过的(hooks.rb):
pre $ $
在之前的
中放入了内部钩子
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
输出结果为:
内部钩子在
之前Selenium :: WebDriver :: Error :: WebDriverError:无法找到chromedriver可执行文件。请从http://chromedriver.storage.googleapis.com/index.html下载服务器,并将其放置在PATH的某处。有关详情,请访问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:在`executable_path'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/service.rb:34:在`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:在`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:在'for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir -webdriver-0.6.11 / lib / watir-webdriver / browser.rb:46:在`initialize'
C:/Users/Admin/watircukepractice/test_puppies/features/support/hooks.rb:11:在`新的
C:/ Users / Admin / watircukepractice / test_puppi es / features / support / hooks.rb:11:在'之前'
我在Windows 7上,使用Ruby版本1.9.3p551,我指的是教程 http://watirwebdriver.com/chrome/ 。
如何告诉Watir(和Selenium-WebDriver)chromedriver.exe的位置?
解决方案1 - Selenium :: WebDriver :: Chrome.driver_path =有一个 Selenium :: WebDriver :: Chrome.driver_path =
允许指定chromedriver二进制文件的方法:
#指定驱动程序路径
chromedriver_path = File.join(File.absolute_path('../ ..',File.dirname(__ FILE__)), 浏览器,chromedriver.exe)
Selenium :: WebDriver :: Chrome.driver_path = chromedriver_path
#正常启动浏览器
b = Watir :: Browser.new :chrome
b.goto'www.google.co m'
b.close
解决方案2 - 在浏览器初始化时指定:driver_path b 另外,您还可以在初始化浏览器时指定驱动程序路径。这样更好一些,因为你不需要使用Selenium代码,但是如果你在不同的地方初始化浏览器,它会重复使用。 解决方案3 - 更新ENV ['PATH'] 当我最初回答这个问题时无论出于何种原因,我都无法获得上述解决方案。当Selenium-WebDriver启动驱动程序时,设置该值似乎没有被使用。虽然第一种解决方案是推荐的方法,但这是一种替代方案。
chromedriver_path = File.join(File.absolute_path('../ ..',File.dirname(__ FILE __)),browsers,chromedriver.exe)
#使用驱动程序路径初始化浏览器
browser = Watir :: Browser.new:chrome,driver_path:chromedriver_path
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}如果Windows?
binary_names.each do | binary_name |
paths.each do | path |
exe = File.join(path,binary_name)
如果File.exe可用则返回exe?(exe)
结束
结束
nil
结束
要指定包含二进制文件的文件夹,只需更改 ENV ['PATH']
(追加目录):
require'watir'
#确定包含chromedriver.exe
的目录chromedriver_directory = File.join(File.absolute_path('../ ..',File.dirname(__ FILE __)),browsers)
#将该目录添加到路径
ENV ['PATH'] =#{ENV ['PATH']}#{File :: PATH_SEPARATOR}#{chromedriver_directory}
#正常启动浏览器
b = Watir :: Browser.new:chrome
b.goto'www.google.com'
b.close
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 machine so I can't hardcode the executable path. I also want the binary to be kept with our code on Git rather than making each test engineer manually update the binary as newer versions are released.
Now I have placed Chrome binary at a absolute path, but it is not being 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
The output is:
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 am referring to tutorial http://watirwebdriver.com/chrome/.
How do I tell Watir (and Selenium-WebDriver) the location of the chromedriver.exe?
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
这篇关于如何指定chromedriver二进制文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!