Ubuntu java selenium google chrome无法启动新会话

zysjyyx4  于 2023-08-01  发布在  Go
关注(0)|答案(1)|浏览(189)

平台:Ubuntu 22.04 Java版本:17.0.7 selenium :4.10.0 Chrome和chromedriver版本:114.0.5735
1.创建ChromeDriver与谷歌 chrome 和chromedriver安装在主机系统
我通过这个测试了Google Chrome连接:

System.setProperty("webdriver.chrome.driver", chromeWebdriver);

ChromeOptions co = new ChromeOptions();
co.addArguments("--remote-allow-origins=*");
co.addArguments("--headless=new");
co.addArguments("--allowed-ips=127.0.0.1");
co.addArguments("--no-sandbox");
co.addArguments("--disable-setuid-sandbox");
co.addArguments("--remote-debugging-port=9222");
co.addArguments("--disable-dev-shm-usage");
co.addArguments("--disable-extensions");
co.addArguments("--disable-gpu");

driver = new ChromeDriver(co);
driver.get(W3SCHOOL_URL);
logger.info("test_chrome_by_system_property page title: {}", driver.getTitle());

字符串
1.使用Google Chrome和Chromedriver Dockerfile创建Chromedriver

## install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

## install chrome webdriver
RUN wget -q -O $APP_DIR/chromedriver_$CHROME_DRIVER_VERSION_linux64.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip
RUN unzip $APP_DIR/chromedriver_$CHROME_DRIVER_VERSION_linux64.zip -d /usr/bin/
RUN chmod +x /usr/bin/chromedriver
RUN rm -rf $APP_DIR/chromedriver_$CHROME_DRIVER_VERSION_linux64.zip


我在docker上检查了google chrome和chromedriver的状态,返回如下:
docker exec -it my_docker_container_name google-chrome --help Google Chrome 114.0.5735.198 docker exec -it my_docker_container_name chromedriver --version ChromeDriver 114.0.5735.90(386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052})
但总是发生同样的错误。

Exception:
2023-07-12 13:15:05 [main] INFO  c.e.maventest.MavenTestApplication.logStarted(61) - Started MavenTestApplication in 18.556 seconds (JVM running for 21.414)
Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 45237
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
[1689167707.191][SEVERE]: bind() failed: Cannot assign requested address (99)
ChromeDriver was started successfully.
2023-07-12 13:15:12 [main] ERROR c.e.m.service.SpringSeleniumRunner.run(101) - SpringSeleniumRunner exception: {}
java.lang.Exception: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed.
  (unknown error: unable to discover open pages)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: '4.1.4', revision: '535d840ee2'
System info: host: '247af48ef4bc', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit-pr', java.version: '17.0.7'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --headless=new, --allowed-ips=127.0.0.1, --no-sandbox, --disable-setuid-sandbox, --remote-debugging-port=9222, --disable-dev-shm-usage, --disable-extensions, --disable-gpu], extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --headless=new, --allowed-ips=127.0.0.1, --no-sandbox, --disable-setuid-sandbox, --remote-debugging-port=9222, --disable-dev-shm-usage, --disable-extensions, --disable-gpu], extensions: []}}}]
    at com.example.maventest.service.SpringSeleniumRunner.testChromeBySystemProperty(SpringSeleniumRunner.java:60)
    at com.example.maventest.service.SpringSeleniumRunner.run(SpringSeleniumRunner.java:98)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:749)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
    at com.example.maventest.MavenTestApplication.main(MavenTestApplication.java:13)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed.
  (unknown error: unable to discover open pages)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: '4.1.4', revision: '535d840ee2'
System info: host: '247af48ef4bc', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit-pr', java.version: '17.0.7'

l0oc07j2

l0oc07j21#

你需要考虑几件事:

  • --disable-gpu最初仅用于windows环境。系统是非windows系统,您需要删除参数。
  • --remote-allow-origins=*在Chrome和ChromeDriver * 版本中不再是强制性的:114.0* 所以你可以放弃了。
  • --disable-extensions在现代google-chrome浏览器中不再有效。所以你可以放弃了。
  • --no-sandbox不是强制性的,除非您是以root用户的身份执行测试。所以你可以放弃了。
  • disable-dev-shm-usage--disable-setuid-sandbox是无效的,除非使用--no-sandbox参数。所以你可以放弃了。

进行上述调整执行您的代码。

相关问题