【问题标题】:ruby cron selenium-webdriver errorruby cron selenium-webdriver 错误
【发布时间】:2018-04-12 13:11:39
【问题描述】:

我正在尝试使用 cron 作业运行 ruby​​ 代码,并且我正在使用 whenever gem。

这是我在 .rb 文件中的简单代码

require "watir"
require "selenium-webdriver"
browser = Watir::Browser.start('https://www.google.com',:firefox)
sleep 5 
browser.close

我的 crontab -l 输出是:

  PATH=/home/ba/bin:/home/ba/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

49 16 * * * /bin/bash -l -c 'which ruby >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'which gem >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'which rails >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'which bundle >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'cd /home/ba/Desktop/job_1 && ruby job_2.rb >> /home/ba/Desktop/RoR/cron1.log 2>&1'

# End Whenever generated tasks for: /home/ba/Desktop/RoR/site_p/config/schedule.rb at: 2018-04-12 16:47:41 +0400

cron1.log 显示:

/usr/local/bin/rails
/usr/local/bin/ruby
/usr/local/bin/gem
/usr/local/bin/bundle
/usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok': Process unexpectedly closed with status 1 (Selenium::WebDriver::Error::UnknownError)
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/common.rb:81:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/common.rb:81:in `create_response'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/default.rb:104:in `request'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/bridge.rb:164:in `execute'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/bridge.rb:97:in `create_session'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/firefox/marionette/driver.rb:50:in `initialize'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/firefox/driver.rb:31:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/firefox/driver.rb:31:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/common/driver.rb:52:in `for'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver.rb:85:in `for'
    from /usr/local/lib/ruby/gems/2.5.0/gems/watir-6.10.3/lib/watir/browser.rb:48:in `initialize'
    from /usr/local/lib/ruby/gems/2.5.0/gems/watir-6.10.3/lib/watir/browser.rb:30:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/watir-6.10.3/lib/watir/browser.rb:30:in `start'
    from job_2.rb:3:in `<main>'
  • ruby 2.5.1p57(2018-03-29 修订版 63029)[x86_64-linux] 没有 rvm
  • Rails 5.1.6
  • Firefox 59.0.2(64 位)

    宝石'女仆' gem 'selenium-webdriver'

当我尝试使用 rails runner 运行 cron 作业时出现同样的错误

代码在终端上运行良好。 *只有当我尝试使用 cron 作业运行它时才会出现问题! *

我也尝试不使用任何宝石。即使使用新鲜的 ubuntu 16、ruby 和 rails。

【问题讨论】:

  • 你安装了最新的geckodriver吗?
  • 使用旧版驱动,不要使用 gecodriver。
  • 不要使用旧版驱动程序。 Geckodriver 本质上是功能完整且与 Selenium 4 兼容的。尝试使用Selenium::WebDriver.logger.level = :debug 运行,它将为您提供有关导致该错误的原因的更多信息。也可以尝试使用webdrivers gem,这可能是最新驱动程序可用的问题。
  • @titusfortner 我不确定您是否实际实现了任何脚本,geckodriver 甚至无法与旧版驱动程序相提并论。我正在使用自动化,我在公司获得报酬

标签: ruby-on-rails ruby selenium cron watir


【解决方案1】:

cron 在执行时丢失了 DISPLAY 环境变量。

将 ENV['DISPLAY'] = ":0" 这个添加到我的脚本修复了我的情况

详情:

  1. 我将此 Selenium::WebDriver.logger.level = :debug 添加到我的脚本中(非常感谢@titusfortner)

新日志:

2018-04-13 09:30:01 DEBUG Selenium Executing Process ["/home/ba/.local/bin/geckodriver", "--binary=/usr/bin/firefox", "--port=4444"]
1523597401454   geckodriver INFO    geckodriver 0.20.1
1523597401458   geckodriver INFO    Listening on 127.0.0.1:4444
1523597401713   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.cG0M1jGsI4Zn"
Error: no DISPLAY environment variable specified
/usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok': Process unexpectedly closed with status 1 (Selenium::WebDriver::Error::UnknownError)
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'

我看到了:错误:未指定 DISPLAY 环境变量

  1. 因为驱动程序在 shell 和 rails c 上运行良好 我运行它来为 DISPLAY 找到正确的值 echo $DISPLAY 或 from rails c puts ENV['DISPLAY'] 结果是“:0”

  2. 所以我把它添加到我的脚本中,问题就解决了

    ENV['DISPLAY'] = ":0"

非常感谢您的好建议 stackoverflow 超级好用

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 2012-07-31
    • 2016-04-05
    • 2015-08-26
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多