Selenium Chrome无法启动:在远程服务器上使用Apache Tomcat/10.0.23中的chromedriverin时崩溃

htrmnn0y  于 2023-02-17  发布在  Go
关注(0)|答案(1)|浏览(433)

我在一个实现Selenium chromedriver的远程服务器上运行Apache Tomcat/10.0.23中的Java Web应用程序。但是,当我创建Web驱动程序时,它返回HTTP状态500-内部服务器错误,但在日志中显示ChromeDriver已成功启动。
谷歌浏览器位置:/usr/bin/谷歌浏览器/usr/共享/man/man1/谷歌浏览器
谷歌 chrome 版:110.0.5481.77
Chrome驱动程序位置:/usr/bin/内存驱动程序
Chrome驱动程序版本:110.0.5481.77(65ed616c6e8ee3fe0ad64fe83796c020644d42af-refs/分支头端/5481@{#839})
小服务程序:

import java.io.IOException;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet(name = "TestServlet", urlPatterns = { "/test" })
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
        ChromeOptions options = new ChromeOptions();
        options.setPageLoadStrategy(PageLoadStrategy.EAGER);
        options.addArguments("--headless=new");
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--no-sandbox");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.google.com/");
        System.out.println("Page Title: " + driver.getTitle());
        driver.quit();
    }
}

错误:

<body>
    <h1>HTTP Status 500 – Internal Server Error</h1>
    <hr class="line" />
    <p><b>Type</b> Exception Report</p>
    <p><b>Message</b> unknown error: Chrome failed to start: crashed.</p>
    <p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.
    </p>
    <p><b>Exception</b></p>
    <pre>org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed.
  (unknown error: DevToolsActivePort file doesn&#39;t exist)
  (The process started from chrome location &#47;usr&#47;bin&#47;google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: &#39;3.14.0&#39;, revision: &#39;aacccce0&#39;, time: &#39;2018-08-02T20:19:58.91Z&#39;
System info: host: &#39;ip-xxxxxxxxxxx;, ip: &#39;127.0.0.1&#39;, os.name: &#39;Linux&#39;, os.arch: &#39;amd64&#39;, os.version: &#39;5.10.0-21-cloud-amd64&#39;, java.version: &#39;11.0.15.1&#39;
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x5642671afd93 &lt;unknown&gt;
#1 0x564266f7e2d7 &lt;unknown&gt;
#2 0x564266fa6ab0 &lt;unknown&gt;
#3 0x564266fa2a3d &lt;unknown&gt;
#4 0x564266fe74f4 &lt;unknown&gt;
#5 0x564266fde353 &lt;unknown&gt;
#6 0x564266fade40 &lt;unknown&gt;
#7 0x564266faf038 &lt;unknown&gt;
#8 0x5642672038be &lt;unknown&gt;
#9 0x5642672078f0 &lt;unknown&gt;
#10 0x5642671e7f90 &lt;unknown&gt;
#11 0x564267208b7d &lt;unknown&gt;
#12 0x5642671d9578 &lt;unknown&gt;
#13 0x56426722d348 &lt;unknown&gt;
#14 0x56426722d4d6 &lt;unknown&gt;
#15 0x564267247341 &lt;unknown&gt;
#16 0x7f086945dea7 start_thread

    java.base&#47;jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    java.base&#47;jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    java.base&#47;jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.base&#47;java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
    org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
    org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:122)
    java.base&#47;java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    java.base&#47;java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    java.base&#47;java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
    java.base&#47;java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
    java.base&#47;java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
    java.base&#47;java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    java.base&#47;java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
    java.base&#47;java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    java.base&#47;java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:543)
    org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)
    org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
    org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
    org.openqa.selenium.remote.RemoteWebDriver.&lt;init&gt;(RemoteWebDriver.java:130)
    org.openqa.selenium.chrome.ChromeDriver.&lt;init&gt;(ChromeDriver.java:181)
    org.openqa.selenium.chrome.ChromeDriver.&lt;init&gt;(ChromeDriver.java:168)
    org.openqa.selenium.chrome.ChromeDriver.&lt;init&gt;(ChromeDriver.java:157)
    com.fourdcontrols.servlets.TestServlet.doGet(TestServlet.java:29)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:668)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
</pre>
    <p><b>Note</b> The full stack trace of the root cause is available in the server logs.</p>
    <hr class="line" />
    <h3>Apache Tomcat/10.0.23</h3>
</body>
    • 日志:**
Starting ChromeDriver 110.0.5481.77 (65ed616c6e8ee3fe0ad64fe83796c020644d42af-refs/branch-heads/5481@{#839}) on port 29602
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

我试过旧版本的chrome驱动程序和谷歌浏览器,但得到相同的错误.

qvk1mo1f

qvk1mo1f1#

此错误消息...

The server encountered an unexpected condition that prevented it from fulfilling the request

...表示ChromeDriver无法生成新的 * 浏览上下文 *,即google-chrome会话。
您的主要问题是--headless=new参数/功能与您正在使用的Selenium版本之间的不兼容

  • 您正在使用 * Selenium v3.14.0 *
Build info: version: &#39;3.14.0&#39;
  • 但您尝试使用参数***--headless=new***

其中,***--headless=new***参数/功能使用***Selenium v4.8.0***
溶液
要使用参数--headless=new,请确保:

相关问题