我有一个基于 Debian 的 Google Compute Engine 来运行我的 Selenium 脚本,但我无论如何都无法运行任何与 Chrome 交互的脚本。我尝试安装 Chrome 和 Chromium - 以及所有依赖项 - 但没有成功。它仍然失败
webdriverio 脚本:script.js
var webdriverio = require('webdriverio');
var options = { desiredCapabilities: { browserName: 'chrome' } };
var client = webdriverio.remote(options);
client
.init()
.then(()=> {
console.log('init ok')
},
(err)=> {
console.log('init failed', err)
})
.url('https://duckduckgo.com/')
.getTitle().then(function(title) {
console.log('Title is: ' + title);
})
.end();
完整输出node script.js
这基本上是 Selenium 进程的错误的一个子集
init failed { Error
message: 'unknown error: Chrome failed to start: exited abnormally\n (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c15
31b57),platform=Linux 3.16.0-4-amd64 x86_64) (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 60.05 s
econds\nBuild info: version: \'3.4.0\', revision: \'unknown\', time: \'unknown\'\nSystem info: host: \'dev-ACME-no\', ip: \'10.132.0.2\', os.name:
\'Linux\', os.arch: \'amd64\', os.version: \'3.16.0-4-amd64\', java.version: \'1.8.0_121\'\nDriver info: driver.version: ChromeDriver',
type: 'RuntimeError',
seleniumStack:
{ status: 13,
type: 'UnknownError',
message: 'An unknown server-side error occurred while processing the command.',
orgStatusMessage: 'unknown error: Chrome failed to start: exited abnormally\n (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291
d320bdf05c1531b57),platform=Linux 3.16.0-4-amd64 x86_64) (WARNING: The server did not provide any stacktrace information)\nCommand duration or time
out: 60.05 seconds\nBuild info: version: \'3.4.0\', revision: \'unknown\', time: \'unknown\'\nSystem info: host: \'dev-ACME-no\', ip: \'10.132.0.2\
', os.name: \'Linux\', os.arch: \'amd64\', os.version: \'3.16.0-4-amd64\', java.version: \'1.8.0_121\'\nDriver info: driver.version: ChromeDriver'
} }
硒工艺
13:26:19.237 INFO - Selenium build info: version: '3.4.0', revision: 'unknown'
13:26:19.237 INFO - Launching a standalone Selenium Server
2017-06-21 13:26:19.256:INFO::main: Logging initialized @248ms to org.seleniumhq.jetty9.util.log.StdErrLog
13:26:19.306 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the c
urrent platform LINUX
13:26:19.306 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform LINUX
13:26:19.306 INFO - Driver class not found: com.opera.core.systems.OperaDriver
13:26:19.307 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
13:26:19.307 INFO - Driver class not found: com.opera.core.systems.OperaDriver
13:26:19.307 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
13:26:19.308 INFO - Driver provider org.openqa.selenium.safari.SafariDriver registration is skipped:
registration capabilities Capabilities [{browserName=safari, version=, platform=MAC}] does not match the current platform LINUX
2017-06-21 13:26:19.347:INFO:osjs.Server:main: jetty-9.4.3.v20170317
2017-06-21 13:26:19.384:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@55f3ddb1{/,null,AVAILABLE}
2017-06-21 13:26:19.400:INFO:osjs.AbstractConnector:main: Started ServerConnector@1794d431{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2017-06-21 13:26:19.400:INFO:osjs.Server:main: Started @393ms
13:26:19.400 INFO - Selenium Server is up and running
13:26:19.685 INFO - SessionCleaner initialized with insideBrowserTimeout 0 and clientGoneTimeout 1800000 polling every 180000
Selenium started
13:26:24.408 INFO - Executing: [new session: Capabilities [{rotatable=true, locationContextEnabled=true, loggingPrefs=org.openqa.selenium.logging.L
oggingPreferences@71d24c0f, browserName=chrome, javascriptEnabled=true, handlesAlerts=true, requestOrigins={name=webdriverio, version=4.6.2, url=ht
tp://webdriver.io}}]])
13:26:24.435 INFO - Creating a new session for Capabilities [{rotatable=true, locationContextEnabled=true, loggingPrefs=org.openqa.selenium.logging
.LoggingPreferences@71d24c0f, browserName=chrome, javascriptEnabled=true, handlesAlerts=true, requestOrigins={name=webdriverio, version=4.6.2, url=
http://webdriver.io}}]
Starting ChromeDriver 2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57) on port 19256
Only local connections are allowed.
13:27:24.920 WARN - Exception thrown
java.util.concurrent.ExecutionException: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'dev-ACME-no', ip: '10.132.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-4-amd64', java.version: '1.8.0_121'
Driver info: driver.version: unknown
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.openqa.selenium.remote.server.DefaultSession.execute(DefaultSession.java:183)
at org.openqa.selenium.remote.server.DefaultSession.<init>(DefaultSession.java:119)
at org.openqa.selenium.remote.server.DefaultSession.createSession(DefaultSession.java:95)
at org.openqa.selenium.remote.server.DefaultDriverSessions.newSession(DefaultDriverSessions.java:131)
at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:59)
at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:36)
at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:111)
at org.openqa.selenium.remote.server.JsonHttpCommandHandler.handleRequest(JsonHttpCommandHandler.java:190)
at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:222)
at org.openqa.selenium.remote.server.DriverServlet.doPost(DriverServlet.java:184)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.openqa.selenium.remote.server.DriverServlet.service(DriverServlet.java:150)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.seleniumhq.jetty9.servlet.ServletHolder.handle(ServletHolder.java:841)
at org.seleniumhq.jetty9.servlet.ServletHandler.doHandle(ServletHandler.java:543)
at org.seleniumhq.jetty9.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.seleniumhq.jetty9.server.handler.ContextHandler.doHandle(ContextHandler.java:1228)
at org.seleniumhq.jetty9.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.seleniumhq.jetty9.servlet.ServletHandler.doScope(ServletHandler.java:481)
at org.seleniumhq.jetty9.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.seleniumhq.jetty9.server.handler.ContextHandler.doScope(ContextHandler.java:1130)
at org.seleniumhq.jetty9.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.seleniumhq.jetty9.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.seleniumhq.jetty9.server.Server.handle(Server.java:564)
at org.seleniumhq.jetty9.server.HttpChannel.handle(HttpChannel.java:320)
at org.seleniumhq.jetty9.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.seleniumhq.jetty9.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.seleniumhq.jetty9.io.FillInterest.fillable(FillInterest.java:112)
at org.seleniumhq.jetty9.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.seleniumhq.jetty9.util.thread.Invocable.invokePreferred(Invocable.java:122)
at org.seleniumhq.jetty9.util.thread.strategy.ExecutingExecutionStrategy.invoke(ExecutingExecutionStrategy.java:58)
at org.seleniumhq.jetty9.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:201)
at org.seleniumhq.jetty9.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:133)
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'dev-ACME-no', ip: '10.132.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-4-amd64', java.version: '1.8.0_121'
Driver info: driver.version: unknown
at org.openqa.selenium.remote.server.DefaultDriverProvider.callConstructor(DefaultDriverProvider.java:110)
at org.openqa.selenium.remote.server.DefaultDriverProvider.newInstance(DefaultDriverProvider.java:96)
at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance(DefaultDriverFactory.java:60)
at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:222)
at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:209)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:176)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
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.server.DefaultDriverProvider.callConstructor(DefaultDriverProvider.java:102)
... 9 more
Caused by: org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 3.16.0-4-amd64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.05 seconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'dev-ACME-no', ip: '10.132.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-4-amd64', java.version: '1.8.0_121'
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:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
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$22(ProtocolHandshake.java:365)
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:368)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:184)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:148)
... 14 more
13:27:24.924 WARN - Exception: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 3.16.0-4-amd64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.05 seconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'dev-ACME-no', ip: '10.132.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-4-amd64', java.version: '1.8.0_121'
Driver info: driver.version: ChromeDriver
答案1
当试图缩小问题范围时,启蒙之路就开始了。
脚本必须至少运行以下部分:
- Selenium 及其朋友(
webdriverio
、ChromeDriver 等) - 必须安装 Chrome 及其依赖项
- Xvfb 必须工作
- Chrome 必须在 Xvfb 上运行
- 当通过 Selenium 触发时,Chrome 必须以某种方式在 Xvfb 上运行
解决方法:似乎selenium-standalone start
没有错误,所以 1 没问题。2、3 和 4 似乎工作正常xvfb-run google-chrome --disable-gpu
。不确定 5 是否可行...
指向 Chrome + Selenium 组合方向的一个指针不起作用是输出中的一行:
Chrome failed to start: exited abnormally
我们知道,只要 X 到位,Chrome 就能正常工作。我们也知道,如果没有 X,Chrome 就会失败。
为了让 5 运行,我需要以某种方式将 Xvfb 注入其中,这给我留下了两个选择。要么包装节点进程,要么包装 selenium 进程。第一个没有用,但第二个有用!
在一个终端中:
xvfb-run selenium-standalone start
在第二个终端:
node /tmp/script.js
最后我得到了正确的答案
init ok
Title is: DuckDuckGo
关于设置此项的一些额外调试帮助
当尝试调试错误时,查看 Selenium 的异常堆栈跟踪是值得的。
当你收到此信息时,你可以确定你缺少 Chrome,或者它没有安装
Caused by: org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 3.16.0-4-amd64 x86_64) (WARNING: The server did not provide any stacktrace information)
如果您没有适合 Chromedriver 的 Chrome 版本,例如您只安装了 Chromium(我的情况!),您将看到此错误:
Caused by: org.openqa.selenium.SessionNotCreatedException: session not created exception: Chrome version must be >= 58.0.3029.0
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 3.16.0-4-amd64 x86_64) (WARNING: The server did not provide any stacktrace information)
确保它google-chrome-stable
在$PATH
并且您可以在无头系统上成功执行它。
有关设置的更多提示 查看这个自动设置. 很多很好的指点。