Chrome 没有开始使用 Watir

Chrome not starting using Watir

该脚本在本地有效,但在服务器上无效。

b = Watir::Browser.new :chrome, headless: true

错误:

response.rb:69:in `assert_ok': unknown error: Chrome failed to start: exited abnormally (Selenium::WebDriver::Error::UnknownError)
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 4.9.75-29.el7.x86_64 x86_64)
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `new'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `create_response'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/default.rb:104:in `request'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:62:in `call'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/bridge.rb:166:in `execute'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/bridge.rb:99:in `create_session'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/bridge.rb:53:in `handshake'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/chrome/driver.rb:49:in `initialize'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb:44:in `new'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb:44:in `for'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver.rb:86:in `for'
        from /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/watir-6.16.5/lib/watir/browser.rb:46:in `initialize'
        from rebooter.rb:9:in `new'
        from rebooter.rb:9:in `<main>'

我发现很少有提到 --disable-dev-shm-usage 的答案,但我真的不知道如何通过它。

我试过没有成功:

Watir::Browser.new :chrome, headless: true, disableDevShmUsage: true

我不太确定 shm 是什么,但 df 显示我对它的使用率为 0%:

# df -h | grep -Ei 'shm|use%'
Filesystem                      Size  Used Avail Use% Mounted on
tmpfs                           962M     0  962M   0% /dev/shm

如何在服务器上也将脚本修改为 运行?为什么它在本地工作,但在服务器上不工作?

结合自己的发现,可以这样传args:

args = %w[headless disable-gpu disable-dev-shm-usage disable-software-rasterizer no-sandbox]
b = Watir::Browser.new :chrome, args: args

shmshared memory.
的缩写 关于sandbox

如果您使用的是远程 chromdriver,那么上述传递参数的简单方法将不起作用,那么:

b = Watir::Browser.new :chrome, {url: "http://127.0.0.1:9515", chromeOptions: {args: args}}

http://127.0.0.1:9515 是远程 chromedriver 服务器的示例。

注意 no-sandbox 如果您有其他解决方案,不推荐使用。
所以最好修复它或改用其他浏览器。
firefox现在也有headless模式,结合geckodriver也是一个不错的选择。