兆鹏带你玩转selenium-webdriver——让chrome跑起来(答疑篇)

来源:互联网 发布:电脑练字软件 编辑:程序博客网 时间:2024/04/30 00:46

申明:《兆鹏带你玩转selenium-webdriver》系列文章请勿以任何形式转载、发表,如需转载请联系QQ:252413619


问题:执行以下语句报错

require 'rubygems'require 'selenium-webdriver'chrome=Selenium::WebDriver.for :chrome

错误:Unable to find the chromedriver executable. Please download the server from http://code.google.com/p/chromium/downloads/list and place it somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver.


分析:

1st.既然它提示没有找到chromedriver,那咱们必须先下载一个(前提你得有个chrome浏览器。。。),下载地址就是http://code.google.com/p/chromium/downloads/list,好了,下载完成后将chromedriver.exe存放在C:\Documents and Settings\Administrator\Local Settings\Application Data\Google\Chrome\Application,因为你的chrome浏览器也是安装在这里的。

2nd.他说要place it somewhere on your PATH,那'PATH'是什么呢,其实就是系统环境变量里的‘path’变量,请看以下代码:

def self.executable_path          @executable_path ||= (            Platform.find_binary "chromedriver" or raise Error::WebDriverError, MISSING_TEXT            #Platform.find_binary "chrome" or raise Error::WebDriverError, MISSING_TEXT          )        end

这段代码存放在chrome/service.rb,他又调用platform.find_binary函数,传入参数'chromdriver',我们再看看find_binary函数
def find_binary(*binary_names)      puts 'names:'      puts *binary_names        paths = ENV['PATH'].split(File::PATH_SEPARATOR)        puts 'paths:'        puts paths        binary_names.map! { |n| "#{n}.exe" } if windows?        puts 'name2:'        puts binary_names        binary_names.each do |binary_name|          paths.each do |path|            exe = File.join(path, binary_name)            puts 'result'            puts File.join(path, binary_name)            return exe if File.executable?(exe)          end        end        nil      end

在这里可以验证,错误提示中说的PATH就是环境变量,因此我们需要在环境变量的PATH中添加这个chromedriver的地址,即添加C:\Documents and Settings\Administrator\Local Settings\Application Data\Google\Chrome\Application;切记不要追加chromedriver.exe!


PS:也许你在添加环境变量后,仍没有反应,那就把你当请的cmd窗口关闭,再重新打开一个cmd窗口再执行。


感谢ChenCheng提出问题!欢迎大家提出问题!

原创粉丝点击