I am trying to fetch title of the website "https://www.google.com/" in chrome browser using selenium on Docker. However whenever I execute I get the following exception
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Actual Code
`package remoteTesting.DockerValidation;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class ChromeStandAloneTest {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://172.17.0.2:4444/wd/hub");
DesiredCapabilities cap =new DesiredCapabilities();
cap.setCapability("browserName","chrome");
WebDriver remoteWebDriver = new RemoteWebDriver(url,cap);
remoteWebDriver.get("https://www.google.com/");
System.out.print(remoteWebDriver.getTitle());
}
}`
Maven dependency for selenium
`<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.10.0</version>
</dependency>`
Docker Logs
`[node]
session-timeout = "300"
override-max-sessions = false
detect-drivers = false
drain-after-session-count = 0
max-sessions = 1
[[node.driver-configuration]]
display-name = "chrome"
stereotype = '{"browserName": "chrome", "browserVersion": "114.0", "platformName": "Linux"}'
max-sessions = 1
Starting Selenium Grid Standalone...
Tracing is disabled
14:28:51.150 INFO [LoggingOptions.configureLogEncoding] - Using the system default encoding
14:28:51.212 INFO [OpenTelemetryTracer.createTracer] - Using OpenTelemetry for tracing
14:28:58.003 INFO [NodeOptions.getSessionFactories] - Detected 4 available processors
14:28:58.489 INFO [NodeOptions.report] - Adding chrome for {"browserName": "chrome","browserVersion": "114.0","platformName": "linux","se:noVncPort": 7900,"se:vncEnabled": true} 1 times (Host)
14:28:58.631 INFO [Node.<init>] - Binding additional locator mechanisms: relative
14:28:58.805 INFO [GridModel.setAvailability] - Switching Node 2b995240-198b-47f2-99c3-301922c9a38b (uri: http://172.17.0.2:4444) from DOWN to UP
14:28:58.808 INFO [LocalDistributor.add] - Added node 2b995240-198b-47f2-99c3-301922c9a38b at http://172.17.0.2:4444. Health check every 120s
14:28:59.873 INFO [Standalone.execute] - Started Selenium Standalone 4.10.0 (revision c14d967899): http://172.17.0.2:4444`
Error stack trace
`Jul 29, 2023 8:18:25 PM org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer createTracer
INFO: Using OpenTelemetry for tracing
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" 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: 'Pankajs-MacBook-Air.local', ip: 'fe80:0:0:0:838:973c:93b8:49da%en0'
Build info: version: '4.10.0', revision: 'c14d967899'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '13.4.1', java.version: '18.0.1.1'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome}]}]
Capabilities {browserName: chrome}
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:549)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:227)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:154)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:134)
at remoteTesting.DockerValidation.ChromeStandAloneTest.main(ChromeStandAloneTest.java:16)
Caused by: java.io.UncheckedIOException: java.net.ConnectException: connection timed out: /172.17.0.2:4444
at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:73)
at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42)
at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55)
at org.openqa.selenium.remote.http.netty.NettyHttpHandler.execute(NettyHttpHandler.java:48)
at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42)
at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55)
at org.openqa.selenium.remote.http.netty.NettyClient.execute(NettyClient.java:97)
at org.openqa.selenium.remote.tracing.TracedHttpClient.execute(TracedHttpClient.java:54)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:115)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:96)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:531)
... 4 more
Caused by: java.net.ConnectException: connection timed out: /172.17.0.2:4444
at org.asynchttpclient.netty.channel.NettyConnectListener.onFailure(NettyConnectListener.java:179)
at org.asynchttpclient.netty.channel.NettyChannelConnector$1.onFailure(NettyChannelConnector.java:108)
at org.asynchttpclient.netty.SimpleChannelFutureListener.operationComplete(SimpleChannelFutureListener.java:28)
at org.asynchttpclient.netty.SimpleChannelFutureListener.operationComplete(SimpleChannelFutureListener.java:20)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:590)
at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:583)
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:559)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:492)
at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:636)
at io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:629)
at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:118)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:262)
at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98)
at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:153)
at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /172.17.0.2:4444
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:261)
... 10 more`
Docker command used to download image : docker pull selenium/standalone-chrome:latest
Docker command used to run : docker run -d -p 4444:4444 --shm-size="2g" selenium/standalone-chrome:latest
I have actually tried restarting the Docker, rechecking the selenium version but it did not help me .`
2
Answers
From what I understand you loading the selenium engine through a container, running the java application locally in an IDE (Eclipse, Intelij or Vscode)?
Try changing 172.17.0.2 to localhost
Try using the instruction:
docker run -d -p 4444:4444 -p 7900:7900 –shm-size="2g" selenium/standalone-chrome:latest
Try accessing the selenium container through port 4444 or 7900
In your browser try accessing http://localhost:4444 or http://localhost:7900/?autoconnect=1&resize=scale&password=secret to check if the selenium container is working correctly.
As you are using
RemoteWebDriver
instead of usingselenium-java
dependency:You can use the
selenium-remote-driver
dependency as follows: