Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: After webdriver.close and quit(), reinstantiating ChromeDriver throws SessionNotCreatedException(JAVA) #14435

Closed
Serimert90 opened this issue Aug 24, 2024 · 4 comments

Comments

@Serimert90
Copy link

Serimert90 commented Aug 24, 2024

What happened?

I instantiate my driver as below;

options.setExperimentalOption("excludeSwitches", new String[] { "enable-automation" });
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-blink-features");
options.addArguments("--disable-blink-features=AutomationControlled");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox");
options.addArguments("--start-maximized");
options.addArguments("--user-agent=" + userAgent);
options.addArguments("--disable-web-security");
options.addArguments("--user-data-dir=" + dataDir);
FileUtils.deleteDirectory(dataDirFile); // i always empty the datadir folder at options part
driver = new ChromeDriver(chromeService, options);

then i quit as below;

driver.close();
driver.quit();
driver = null;

After quitting like above, when i try to reinstantiate with same options i get SessionNotCreatedException

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

What i tried both with or without below;

options.setExperimentalOption("detach", true);
options.addArguments("--remote-debugging-port=" + stealthBotOptions.getRemoteDebuggingPortNext());

// tried stopping service too
chromeService.stop();
chromeService.close();

Note: I also tried to give different port everytime i "instantiate"

I also tried killing all chrome related processes (in quit as above) as below

Process process = Runtime.getRuntime().exec("cmd.exe /c taskkill /im chromedriver.exe /f & ver > nul");
process.waitFor();
process.destroy();
process = Runtime.getRuntime().exec("cmd.exe /c taskkill /im chrome.exe /f & ver > nul");
process.waitFor();
process.destroy();

How can we reproduce the issue?

Try my instantiating code like above.

When i start in debug mode in intellij, THIS bug never happens. It only happens in fat jar or normal run in idea.
Found the culprit: Try instantiating chrome driver object with chromeService. Still it is weird that idea debug mode is healthier than normal mode?
---> Broken

if (chromeService != null)
            driver = new ChromeDriver(chromeService, getChromeOptions());

---> Ok

else
           driver = new ChromeDriver(getChromeOptions());

Verdict: After chromeService or driver.stopped, the chromeservice also needs to be reinstantiated with its builder before given to Driver such as new ChromeDriverService.Builder().build();

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. 
Host info: host: 'Some pc', ip: '******' (192 ip not localhost)
Build info: version: '4.23.1', revision: '656257d8e9'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.12'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--start-maximized, --disable-web-security, --disable-blink-features, --disable-blink-features=Au..., --disable-dev-shm-usage, --no-sandbox, --user-agent=Mozilla/5.0 (W..., user-agent=Mozilla/5.0 (Win..., --remote-debugging-port=9402, --user-data-dir=C:/DEV/sele...], excludeSwitches: [enable-automation], extensions: [], useAutomationExtension: false}, pageLoadStrategy: normal, proxy: Proxy(manual, http=14a19aaf...}]}]
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:563)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:245)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:174)
	at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:114)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83)
	at

Operating System

Windows 10

Selenium version

4.23.1

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

Chrome 128

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

Chrome 128

Are you using Selenium Grid?

No response

Copy link

@Serimert90, 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!

@Serimert90 Serimert90 changed the title [🐛 Bug]: After webdriver.close and quit(), reinstantiating ChromeDriver throws SessionNotCreatedException [🐛 Bug]: After webdriver.close and quit(), reinstantiating ChromeDriver throws SessionNotCreatedException(JAVA) Aug 24, 2024
@pujagani
Copy link
Contributor

Thank you for the details. If you are instantiating a ChromeDriverService and then stopping it. The expectation would be to manage its lifecycle if you want to reuse it again. So once you stop it, and reinstantiate the same ChromeDriver instance with the same service that is stopped, it won't work. You will need to start the ChromeDriverService again and pass that.

Your verdict is correct.

I might have misunderstood so please help me understand your concern here.

@Serimert90
Copy link
Author

Thanks for clarifying. If it is expected, i have nothing to say.

Best Regards

@Serimert90
Copy link
Author

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants