【问题标题】:Selenium ruby script fails with chromedriver while running it headlessSelenium ruby​​ 脚本在无头运行时使用 chromedriver 失败
【发布时间】:2014-01-07 20:26:25
【问题描述】:

我正在尝试通过 linux 中的命令提示符运行 selenium ruby​​ 文件 (.rb)。我只需要启动 chrome 并获取一个 url。所以我下载了 Selenium-serverstandalone-2.37.0.jar 和 chromedriver(从 chromedriver_linux(32) 中提取)在同一个目录 (/home/) 中。我也设置了指向 chromedriver 的路径。我是初学者,如果我在这里遗漏了什么,请告诉我。

这是我的 test.rb 文件:

require "selenium-webdriver"
require "rspec"
include RSpec::Expectations

describe "TestScript" do

  before(:each) do
    @driver = Selenium::WebDriver.for :chrome
    @base_url = "http://www.google.com"
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 5
    @verification_errors = []
  end

  after(:each) do
    @driver.quit
    @verification_errors.should == []
  end

  it "test_script" do
    @driver.get(@base_url)
    puts "Logged in"
    if(element_present?(:link, "Home"))
    puts "Home page is detected"
  end
   puts "Logging out"
 end

def element_present?(how, what)
    @driver.find_element(how, what)
    true
    rescue Selenium::WebDriver::Error::NoSuchElementError
    false
end

def verify(&blk)
  yield
  rescue ExpectationNotMetError => ex
  @verification_errors << ex
end

当我运行它时,它会抛出这个错误:

NoMethodError: undefined method 'quit' for Nil:NilClass occured at /home/test.rb

 Failure/Error: @driver = Selenium::WebDriver.for :chrome
 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.

请指导我如何进行!

【问题讨论】:

    标签: ruby selenium rspec selenium-chromedriver


    【解决方案1】:

    chromedriver 可执行文件放在同一目录中是不够的。您必须确保它位于您的$PATH 中的目录中。您可以将其放入 /usr/local/bin 并确保 /usr/local/bin$PATH 中。

    您可以使用

    检查/usr/local/bin 是否已经在您的$PATH
    echo $PATH | grep "/usr/local/bin"
    

    如果这没有输出,您需要将以下行添加到您的 shell 配置中(例如,~/.bashrc,如果您使用的是 Bash):

    export PATH=/usr/local/bin:$PATH
    

    然后重新启动你的 shell 并再次检查。

    【讨论】:

    • 感谢您的快速回复。我在 $PATH 中添加了适用于 linux 的 chromedriver。但我仍然遇到同样的错误!
    • 您能否建议我进行更多改进?
    【解决方案2】:

    终于搞定了!!切换到旧版本的 selenium-standalone-server 就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2019-09-13
      • 2022-09-30
      • 2019-05-07
      • 1970-01-01
      相关资源
      最近更新 更多