Docker compose中的Selenium和Java:连接问题

huangapple go评论82阅读模式
英文:

Docker compose selenium and java: connection problem

问题

我有一个包含Java项目和Selenium Chrome的Docker Compose文件。我正在尝试从Java代码中连接到Chrome。在http://localhost:4444/wd/hub上连接时出现错误,但当使用http://172.20.0.4:4444/wd/hub时一切正常。这个问题困扰着我,因为我不应该硬编码地址,因为Chrome有时会在不同的地址上启动。以下是我之前见过的一些IP地址:

  • 172.20.0.4
  • 172.17.0.3
  • 172.17.0.4
  • 172.18.0.3
  • 172.18.0.4
  • 172.19.0.3
  • 172.19.0.4

请帮助我正确连接到Chrome容器,以使Java代码不依赖于不同的Chrome启动地址。

英文:

I have docker-compose with java project and selenium chrome

version: "3.9"

services:
  web:
    container_name: web
    image: web:latest
    ports:
      - "8080:8080"
    build:
      dockerfile: Dockerfile
      context: .
      args:
        - JAR_FILE=target/*.jar
    depends_on:
      - chrome
    networks:
      - network

  chrome:
    image: seleniarm/standalone-chromium:latest
    shm_size: 2g
    privileged: true
    restart: 'always'
    ports:
      - "4444:4444"
      - "5900:5900"
      - "7900:7900"
    networks:
      - network

I'm trying to connect to chrome from java

        WebDriver webDriver;
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setBrowserName("chrome");
        dc.setPlatform(Platform.LINUX);
        try {
            URI uri = URI.create("http://localhost:4444/wd/hub");
            webDriver = new RemoteWebDriver(uri.toURL(), dc);
        } catch (Exception e) {
            log.error("Couldn't connect WEB_DRIVER");
            e.printStackTrace();
            throw new RuntimeException(e);
        }

This url http://localhost:4444/wd/hub gives me error and this is not because RemoteWebDriver, i have tried different java drivers and got same error

Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
2023-03-08T09:01:20.132826587Z Host info: host: 'c679ce5cf46d', ip: '172.20.0.6'
2023-03-08T09:01:20.132828628Z Build info: version: '4.5.3', revision: '4b786a1e430'
2023-03-08T09:01:20.132830462Z System info: os.name: 'Linux', os.arch: 'aarch64', os.version: '5.10.124-linuxkit', java.version: '17.0.2'
2023-03-08T09:01:20.132832545Z Driver info: org.openqa.selenium.remote.RemoteWebDriver
2023-03-08T09:01:20.132834462Z Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, platformName: LINUX}], desiredCapabilities=Capabilities {browserName: chrome, platformName: LINUX}}]
2023-03-08T09:01:20.132836920Z Capabilities {}
2023-03-08T09:01:20.132839295Z 	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:559) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132841253Z 	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:242) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132843295Z 	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132845420Z 	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:139) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132847462Z 	at com.careerseeker.siteparser.service.impl.SiteParserServiceImpl.getDriver(MyServiceImpl.java:222) ~[classes!/:1.0-SNAPSHOT]
2023-03-08T09:01:20.132849587Z 	... 25 common frames omitted
2023-03-08T09:01:20.132851962Z Caused by: java.io.UncheckedIOException: java.net.ConnectException: Connection refused: localhost/127.0.0.1:4444
2023-03-08T09:01:20.132853920Z 	at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:73) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132856087Z 	at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42) ~[selenium-http-4.5.3.jar!/:na]
2023-03-08T09:01:20.132858087Z 	at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:56) ~[selenium-http-4.5.3.jar!/:na]
2023-03-08T09:01:20.132873837Z 	at org.openqa.selenium.remote.http.netty.NettyHttpHandler.execute(NettyHttpHandler.java:49) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132876128Z 	at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42) ~[selenium-http-4.5.3.jar!/:na]
2023-03-08T09:01:20.132879337Z 	at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:56) ~[selenium-http-4.5.3.jar!/:na]
2023-03-08T09:01:20.132885587Z 	at org.openqa.selenium.remote.http.netty.NettyClient.execute(NettyClient.java:99) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132890628Z 	at org.openqa.selenium.remote.tracing.TracedHttpClient.execute(TracedHttpClient.java:55) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132892628Z 	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:120) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132894587Z 	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:102) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132896545Z 	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132898587Z 	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132900545Z 	at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132902503Z 	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:541) ~[selenium-remote-driver-4.5.3.jar!/:na]
2023-03-08T09:01:20.132904503Z 	... 29 common frames omitted
2023-03-08T09:01:20.132906337Z Caused by: java.net.ConnectException: Connection refused: localhost/127.0.0.1:4444
2023-03-08T09:01:20.132908670Z 	at org.asynchttpclient.netty.channel.NettyConnectListener.onFailure(NettyConnectListener.java:179) ~[async-http-client-2.12.3.jar!/:na]
2023-03-08T09:01:20.132910628Z 	at org.asynchttpclient.netty.channel.NettyChannelConnector$1.onFailure(NettyChannelConnector.java:108) ~[async-http-client-2.12.3.jar!/:na]
2023-03-08T09:01:20.132912628Z 	at org.asynchttpclient.netty.SimpleChannelFutureListener.operationComplete(SimpleChannelFutureListener.java:28) ~[async-http-client-2.12.3.jar!/:na]
2023-03-08T09:01:20.132914712Z 	at org.asynchttpclient.netty.SimpleChannelFutureListener.operationComplete(SimpleChannelFutureListener.java:20) ~[async-http-client-2.12.3.jar!/:na]
2023-03-08T09:01:20.132917170Z 	at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132919128Z 	at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132924087Z 	at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132929170Z 	at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132936920Z 	at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132940170Z 	at io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:609) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132942337Z 	at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:117) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132947003Z 	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:321) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132951045Z 	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:337) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132953587Z 	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132956045Z 	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132958545Z 	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132960670Z 	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132962795Z 	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132965003Z 	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132970420Z 	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132977003Z 	... 1 common frames omitted
2023-03-08T09:01:20.132979128Z Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:4444
2023-03-08T09:01:20.132981045Z Caused by: java.net.ConnectException: Connection refused
2023-03-08T09:01:20.132982878Z 	at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
2023-03-08T09:01:20.132984753Z 	at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
2023-03-08T09:01:20.132987878Z 	at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946) ~[na:na]
2023-03-08T09:01:20.132992837Z 	at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132994878Z 	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.132996878Z 	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.133002295Z 	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.133007712Z 	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.133011212Z 	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.133014253Z 	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.133020003Z 	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.133022045Z 	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.85.Final.jar!/:4.1.85.Final]
2023-03-08T09:01:20.133024212Z 	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

I can see that my chrome starts at http://172.20.0.4:4444

here is some logs from container

Starting Selenium Grid Standalone...
2023-03-08T09:00:02.127960717Z Tracing is disabled
2023-03-08T09:00:03.079255718Z 09:00:03.073 INFO [LoggingOptions.configureLogEncoding] - Using the system default encoding
2023-03-08T09:00:03.082264760Z 09:00:03.081 INFO [OpenTelemetryTracer.createTracer] - Using OpenTelemetry for tracing
2023-03-08T09:00:04.266523135Z 09:00:04.265 INFO [NodeOptions.getSessionFactories] - Detected 5 available processors
2023-03-08T09:00:04.318001468Z 09:00:04.317 INFO [NodeOptions.report] - Adding chrome for {"browserVersion": "110.0","se:noVncPort": 7900,"browserName": "chrome","platformName": "LINUX","se:vncEnabled": true} 1 times
2023-03-08T09:00:04.346006135Z 09:00:04.345 INFO [Node.<init>] - Binding additional locator mechanisms: relative
2023-03-08T09:00:04.381115343Z 09:00:04.380 INFO [GridModel.setAvailability] - Switching Node 57ecd074-632e-4989-ba29-a9b0e61df39e (uri: http://172.20.0.4:4444) from DOWN to UP
2023-03-08T09:00:04.393940177Z 09:00:04.392 INFO [LocalDistributor.add] - Added node 57ecd074-632e-4989-ba29-a9b0e61df39e at http://172.20.0.4:4444. Health check every 120s
2023-03-08T09:00:04.748166885Z 09:00:04.746 INFO [Standalone.execute] - Started Selenium Standalone 4.8.0 (revision 267030adea): http://172.20.0.4:4444

and if i use http://172.20.0.4:4444/wd/hub instead of http://localhost:4444/wd/hub for connection, everything is fine with connection.

This message confusing me -> Connection refused: localhost/127.0.0.1:4444(I didn't specify it anywhere 127.0.0.1)

I can specify url like http://172.20.0.4:4444/wd/hub, but i shouldn't, because sometimes chrome starts at different urls. Here is some IPs i saw before when chrome was starting

172.20.0.4
172.17.0.3
172.17.0.4
172.18.0.3
172.18.0.4
172.19.0.3
172.19.0.4

Connecting via http://localhost:4444/wd/hub only worked when I ran the jar without docker, using a chromedriver file downloaded from the internet, pasting it into the root of the project


Please help to connect to chrome container correctly so that the java code does
not depend on different addresses on which chrome is started.


答案1

得分: 2

问题是您在代码中硬编码了Chrome实例的URL,并将其指向“localhost”。

当容器通过共同的网络连接在一起时,您应该使用服务名称作为DNS名称来连接它。所以在您的情况下,从“web”服务连接到“chrome”服务时,URL应该是http://chrome:4444/wd/hub

英文:

The problem is that you have the url to your chrome instance hardcoded in your code and it points to localhost.

When container are connected over common network you should use service name as DNS name to connect to it. So in your case url should be http://chrome:4444/wd/hub when
connecting from web service to chrome service.

huangapple
  • 本文由 发表于 2023年3月8日 18:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定