首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ruby -webdriver

ruby -webdriver
EN

Stack Overflow用户
提问于 2018-04-12 13:11:39
回答 1查看 472关注 0票数 1

我试图使用cron作业运行ruby代码,并使用whenever gem。

以下是.rb文件中的简单代码

代码语言:javascript
复制
require "watir"
require "selenium-webdriver"
browser = Watir::Browser.start('https://www.google.com',:firefox)
sleep 5 
browser.close

我的crontab -l输出是:

代码语言:javascript
复制
  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

this显示了以下内容:

代码语言:javascript
复制
/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>'
  • Ruby2.5.1p57 (2018-03-29修订版63029) x86_64-linux无rvm
  • Rails 5.1.6
  • Firefox 59.0.2 (64位) gem 'watir‘gem 'selenium-webdriver’

当我试图用rails运行cron作业时,也会出现同样的错误。

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

而且,我也试着不用任何时候的宝石。即使是新鲜的ubuntu 16,红宝石和铁轨。

EN

回答 1

Stack Overflow用户

发布于 2018-04-13 06:35:34

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

将ENV‘’DISPLAY‘= ":0“添加到我的脚本中,修复了我的情况

详情:

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

新日志:

代码语言:javascript
复制
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'

我看到了:错误:没有显示环境变量指定的

  1. 因为驱动程序从shell和rails c中运行得很好,所以我运行这个程序来查找显示回波$DISPLAY的正确值,或者从rails c查找ENV' DISPLAY‘的结果是":0“。
  2. 所以我把这个添加到我的脚本中,问题就解决了 ENV‘’DISPLAY‘= ":0“

非常感谢你的好建议,堆叠溢出是超级棒

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49797513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档