我需要在docker中运行selenium-side-runner。我在dockerfile中写道要安装Google Chrome和googledrive。但是执行代码时,错误如下:
WebDriverError: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
at Object.throwDecodedError (../../usr/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/error.js:550:15)
at parseHttpResponse (../../usr/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/http.js:560:13)
at Executor.execute (../../usr/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/http.js:486:26)
这是我的dockerfile
FROM python:3.6-stretch
# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >>
/etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable
# install chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS
chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
# set display port to avoid crash
ENV DISPLAY=:99
RUN apt-get update
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get install -y nodejs
RUN apt-get install -y npm
RUN npm install -g selenium-side-runner
WORKDIR /app
ENTRYPOINT ["/entrypoint.sh"]
这是我的代码:
for ind, file in enumerate(test_file_list):
file_path = os.path.join(self.save_path, file)
start_time = timezone.now()
result = subprocess.run(
['selenium-side-runner', '-c', "goog:chromeOptions.args=[--headless,--nogpu]
browserName=chrome",file_path])
end_time = timezone.now()
result_code = result.returncode
最佳答案
骇客
我建议不要使用手动安装的浏览器,而建议使用像https://github.com/SeleniumHQ/docker-selenium这样已经可用的图像。
解决方案(推荐)
从架构的 Angular 来看,每个Docker容器应仅具有一个用途。对于您而言,该容器负责运行浏览器和测试。因此,我建议将解决方案分为两个容器:
selenium-side-runner
和.side
文件的容器,它将运行测试基于the docs,
selenium-side-runner
可以与Selenium Grid一起使用。因此,解决方案看起来像
Dockerfile
,添加selenium-side-runner
文件并配置.side
的ENTRYPOINT
。在这里,您还可以将默认URL设置为Selenium Grid,例如selenium-side-runner -w 10 --server http://selenium-browsers:4444/wd/hub
,其中selenium-browsers
将是带有浏览器或Selenium Grid的容器的名称。 docker network create selenium-tests
创建Docker网络docker run -id --shm-size=2g --network selenium-tests --name selenium-browsers selenium/standalone-chrome:3.141.59-2020040
docker run -it --network selenium-tests --name my-tests <your custom image>
运行您的容器由于两个容器都在同一个自定义网络
selenium-tests
中,因此两个容器都可以通过容器名称进行通信。因此,my-tests
容器可以将请求发送到selenium-browsers
容器(请参阅#1和--server
选项)。如果需要,您可以稍后创建docker-compose.yaml文件以简化解决方案的使用。
关于python - 如何在docker中运行selenium-side-runner?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61496129/