SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.53k stars 8.16k forks source link

[🐛 Bug]: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. #11698

Closed cosoc closed 1 year ago

cosoc commented 1 year ago

java jar package will error

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/>
    </parent>
    <groupId>cn.cosoc.x</groupId>
    <artifactId>test</artifactId>
    <version>2.0.0</version>
    <name>test</name>
    <description> a test</description>
    <properties>
        <java.version>17</java.version>
        <repackage.classifier/>
        <spring-native.version>0.11.4</spring-native.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.15</version>
        </dependency>

        <dependency>
            <groupId>com.mortennobel</groupId>
            <artifactId>java-image-scaling</artifactId>
            <version>0.8.6</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.36.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

        <dependency>
            <groupId>com.googlecode.soundlibs</groupId>
            <artifactId>mp3spi</artifactId>
            <version>1.9.5.4</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>cn.cosoc</groupId>
            <artifactId>c2s2c</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.playwright</groupId>
            <artifactId>playwright</artifactId>
            <version>1.28.1</version>
        </dependency>

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.33</version>
        </dependency>

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.assertthat</groupId>
            <artifactId>selenium-shutterbug</artifactId>
            <version>1.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>4.8.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                    <configurationFile>src/main/resources/SqliteMyBatisGenerator.xml</configurationFile>
                </configuration>
                <dependencies>
                    <dependency>
                            <groupId>org.xerial</groupId>
                            <artifactId>sqlite-jdbc</artifactId>
                            <version>3.36.0.3</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <excludes>
                        <exclude>**/config/application-mqtt-dev.yml</exclude>
                        <exclude>**/config/application-mqtt-test.yml</exclude>
                        <exclude>**/config/application-server-dev.yml</exclude>
                        <exclude>**/config/application-server-test.yml</exclude>
                        <exclude>**/application-mqtt-dev.yml</exclude>
                        <exclude>**/application-mqtt-test.yml</exclude>
                        <exclude>**/application-server-dev.yml</exclude>
                        <exclude>**/application-server-test.yml</exclude>
                        <exclude>**/application-c2s2c-dev.yml</exclude>
                        <exclude>**/application-c2s2c-test.yml</exclude>
                        <exclude>**/SqliteMyBatisGenerator.xml</exclude>
                        <exclude>**/application-server-plaintext-prod.xml</exclude>
                        <exclude>**/encryptor.cmd</exclude>
                        <exclude>**/appInfo.json</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.github.ulisesbocchio</groupId>
                <artifactId>jasypt-maven-plugin</artifactId>
                <version>3.0.4</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.cosoc.SnailXApplication</mainClass>
                    <classifier>${repackage.classifier}</classifier>
                    <image>
                        <builder>paketobuildpacks/builder:tiny</builder>
                        <env>
                            <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
                        </env>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

How can we reproduce the issue?

public ChromeDriver getDefaultBrowserChrome(boolean isHeadless, Integer width, Integer height) {
        System.setProperty("webdriver.chrome.driver",APPConfig.getWebDriverFileForChromiumPath().toString());
        // chrome选择
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--user-data-dir=" + APPConfig.getAppChromiumDataPath());
        chromeOptions.addArguments("--load-extension=" + APPConfig.getSnailXBrowserExtendPath());
        chromeOptions.setExperimentalOption("excludeSwitches",new String[]{"enable-automation"});
        chromeOptions.setExperimentalOption("useAutomationExtension", "False");
        if (isHeadless) {
            chromeOptions.addArguments("--headless");
        }
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        if ( width != null  && width > 0 ) {
            if (height != null || height <= 0) {
                height = 800;
            }
            Dimension targetSize = new Dimension(width,height);
            chromeDriver.manage().window().setSize(targetSize);
        }
        chromeDriver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");

        return chromeDriver;
    }

Relevant log output

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

Operating System

debian11

Selenium version

4.8.1

What are the browser(s) and version(s) where you see this issue?

100.0.4896.127

What are the browser driver(s) and version(s) where you see this issue?

The development environment is normal but jar not run

Are you using Selenium Grid?

No response

github-actions[bot] commented 1 year ago

@cosoc, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] commented 1 year ago

Hi, @cosoc. Please follow the issue template, we need more information to reproduce the issue.

Either a complete code snippet and URL/HTML (if more than one file is needed, provide a GitHub repo and instructions to run the code), the specific versions used, or a more detailed description to help us understand the issue.

Note: If you cannot share your code and URL/HTML, any complete code snippet and URL/HTML that reproduces the issue is good enough.

Reply to this issue when all information is provided, thank you.

cosoc commented 1 year ago

果您无法共享您的代码

public ChromeDriver getDefaultBrowserChrome(boolean isHeadless, Integer width, Integer height) {
    System.setProperty("webdriver.chrome.driver", "/home/dev/chromedriver/chromedriver");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--user-data-dir=/home/dev/chromedriver/data");
    chromeOptions.setExperimentalOption("excludeSwitches",new String[]{"enable-automation"});
    chromeOptions.setExperimentalOption("useAutomationExtension", "False");
    if (isHeadless) {
        chromeOptions.addArguments("--headless");
    }
    ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
    // 设置尺寸
    if ( width != null  && width > 0 ) {
        if (height != null || height <= 0) {
            height = 800;
        }
        org.openqa.selenium.Dimension targetSize = new Dimension(width,height);
        chromeDriver.manage().window().setSize(targetSize);
    }
    chromeDriver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");

    return chromeDriver;
}
diemol commented 1 year ago

You cannot remove the webdriver property by the way

chromeDriver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");

What values are you using to invoke getDefaultBrowserChrome? Why don't you provide a complete script?

cosoc commented 1 year ago

This is an encapsulated public method that can be directly called in the main method. There are many places to call, so no call logic code is provided. This code can open the Chrome browser if the idea is running, and the function is normal. However, if the mvn clean package is packaged and the java-jar test.jar is run, an error will be reported. And it only prompts the appeal information and does not stack the information. I use jdk17 to run the jar package. I tried to use the same driver and modify system permissions as the development environment, but still reported an error. Very distressed! Thank you for your reply!

diemol commented 1 year ago

I understand, but how can I triage something if I cannot execute the code with the same parameters you are using? Why don't you provide the main you are referring to?

I ask because I already tried the code and it worked for me, so I guess something is different in the way it is being invoked.

cosoc commented 1 year ago

The reason is that jre uses a customized runtime and does not set the correct permissions (Linux), but why not print the information related to permissions?

github-actions[bot] commented 10 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.