Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

java - org.openqa.selenium.WebDriverException:java.net.ConnectException:无法连接到localhost / 127.0.0.1:9340(org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:9340)

I have an app, that serves HTTP requests and base on the params it runs some basic selenium actions (open web page eg).

(我有一个应用程序,可以处理HTTP请求,并基于参数运行一些基本的硒操作(例如,打开网页)。)

It's written in Scala, what is actually really close to Java implementation.

(它是用Scala编写的,实际上实际上与Java实现非常接近。)

Here is how the code sample looks like:

(代码示例如下所示:)

val firefoxBinary = new FirefoxBinary()
firefoxBinary.addCommandLineOptions("--headless")

val firefoxOptions = new FirefoxOptions()
firefoxOptions.setBinary(firefoxBinary)

val webDriver = new FirefoxDriver(firefoxOptions)
webDriver.get(s"https://google.com")
webDriver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS)
logger.info(s"Title: ${webDriver.getTitle}")
webDriver.close()

When I run it locally, everything goes fine.

(当我在本地运行时,一切正常。)

But when I package it in a docker container, I get this error:

(但是当我将其打包在docker容器中时,出现以下错误:)

Error servicing request: GET /run-selenium from 172.17.0.1 {{ org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:9340
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '4fb27eff9adc', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.184-linuxkit', java.version: '1.8.0_212'
Driver info: driver.version: FirefoxDriver }}
org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:9340
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '4fb27eff9adc', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.184-linuxkit', java.version: '1.8.0_212'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)

I use this docker image as base one:

(我使用此docker映像作为基础映像:)

FROM openjdk:8-jre-alpine3.9
ARG firefox_ver=70.0
ARG geckodriver_ver=0.26.0

# Download and install deps
RUN apk update && apk add curl curl-dev

RUN mkdir /opt/docker && chmod a=rwX /opt/docker

# Download and install Firefox
RUN curl -fL -o /tmp/firefox.tar.bz2 https://ftp.mozilla.org/pub/firefox/releases/${firefox_ver}/linux-x86_64/en-GB/firefox-${firefox_ver}.tar.bz2 
&& tar -xjf /tmp/firefox.tar.bz2 -C /tmp/ && mv /tmp/firefox /opt/docker

# Download and install geckodriver
RUN curl -fL -o /tmp/geckodriver.tar.gz 
https://github.com/mozilla/geckodriver/releases/download/v${geckodriver_ver}/geckodriver-v${geckodriver_ver}-linux64.tar.gz 
&& tar -xzf /tmp/geckodriver.tar.gz -C /tmp/ && chmod +x /tmp/geckodriver && mv /tmp/geckodriver /opt/docker

ENV PATH="/opt/docker/firefox:${PATH}"
  ask by Alex Fruzenshtein translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...