【问题标题】:Running Chromedriver on Ubuntu Server headlessly在 Ubuntu Server 上无头运行 Chromedriver
【发布时间】:2018-03-26 21:13:36
【问题描述】:

我在 Java (1.8) 中使用 Selenium 和 Chromedriver 来进行一些自动网络爬取:

System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("someurl.com");

我正在尝试迁移到 Ubuntu 16.04 服务器。在服务器上,我安装了 Ubuntu chromedriver 2.37 版、chrome 65 版。根据 chromedriver 文档,这些版本是兼容的。我已经更改了指定 Ubuntu 中 chromedriver 位置的代码:

System.setProperty("webdriver.chrome.driver", "/usr/lib/local/chromedriver");

在运行我的程序之前,我启动了 xvfb:

Xvfb -ac :99 -screen 0 1280x1024x16 &
export DISPLAY=:99

然后执行我的程序:

java -jar myprogram.jar

以下内容打印到控制台:

Starting ChromeDriver 2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7) on port 10574
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.4.0-112-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.35 seconds
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:15.31Z'
System info: host: 'ubuntu-s-1vcpu-1gb-nyc1-01', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-112-generic', java.version: '1.8.0_151'
Driver info: driver.version: ChromeDriver
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
        at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$new$0(JsonWireProtocolResponse.java:53)
        at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$getResponseFunction$2(JsonWireProtocolResponse.java:91)
        at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:209)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:132)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
        at RHio.Test.main(Test.java:39)

【问题讨论】:

  • Chrome,从 65 版本开始,支持无头模式。不需要 xvfb。
  • 好的,我已经尝试使用无头ChromeOption,并且没有运行xvfb,但问题仍然存在......
  • 为我工作。如果没有额外的信息,可能会出现一百种不同的错误。阅读如何创建minimal reproducible example
  • 您使用什么版本的 chromedriver & chrome?根据我发现的相关问题,我怀疑我的安装有问题
  • 也许是个愚蠢的问题,但是您安装了 Chrome 吗? (不仅仅是chromedriver,而是实际的浏览器)我猜你没有。

标签: java google-chrome selenium selenium-chromedriver headless


【解决方案1】:

这个错误确实让我们知道发生了什么错误:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally

当您尝试在 Ubuntu Server headless 上启动 ChromedriverChrome 时,您必须通过几个选项如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path='/path/to/chromedriver')
driver.get('http://google.com/')
driver.quit()

附加说明

根据强制性的Requirements#1341 确保以下内容:

  • Chromedriver/usr/local/bin/
  • Chrome 浏览器/usr/bin/google-chrome

tl;博士

Sandbox的故事

【讨论】:

  • 这些选项中的大多数与无头 Chrome 无关。在 Java 中,你可以通过 chromeOptions.setHeadless(true);,我相信 Python 也有类似的东西。另外,如果您通过Chrome(chrome_options=options, executable_path='/path/to/chromedriver'),为什么还必须确保“Chromedriver 在 /usr/local/bin/ 内”?
  • 在重新安装 chromedriver 和 chrome 后,确保它们在这些文件夹中,并添加所有这些 chromeOptions 参数,它就可以工作了!谢谢
  • 就我而言,似乎 --no-sandbox 成功了。谢谢!
  • 谢谢@DebanjanB!!
  • Chromedriver is within /usr/local/bin/ - 似乎不再需要,因为您可以在代码中正确指定它,例如System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver_linux64_v81/chromedriver"); - 为我工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2012-07-25
相关资源
最近更新 更多