簡體   English   中英

紅寶石cron硒webdriver錯誤

[英]ruby cron selenium-webdriver error

我正在嘗試使用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'watir'gem'selenium-webdriver'

當我嘗試運行cron作業和rails賽跑者時也會出現相同的錯誤

代碼從終端運行正常。 *問題僅當我嘗試使用cron job運行時! *

我也嘗試沒有寶石。 即使使用新鮮的ubuntu 16,紅寶石和鐵軌。

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 ci上運行良好,因此可以運行此操作以找到DISPLAY echo $ DISPLAY的正確值,或者從rails c放入ENV ['DISPLAY']結果為“:0”

  2. 所以我將其添加到腳本中,問題已解決

    ENV ['DISPLAY'] =“:0”

非常感謝您的好建議stackoverflow超級好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM