我需要启动selenium
inside docker
容器。将浏览器配置文件传递给很重要webdriver
.
Here's docker-compose
:
version: '2'
services:
worker_main:
build: ./app
volumes:
- /Users/username/Library/Application Support/Google/Chrome/Profile 1:/profile
restart: always
env_file:
- config.env
networks:
- backend
depends_on:
- chrome
chrome:
image: selenium/standalone-chrome
restart: always
ports:
- 4444:4444
hostname: chrome
networks:
- backend
networks:
backend:
这是驱动程序代码:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/profile")
driver = webdriver.Remote("http://chrome:4444/wd/hub", options=options)
结果我发现了这个错误:
selenium.common.exceptions.WebDriverException:消息:未知错误:无法创建默认配置文件目录
你可以尝试这样做:
$ docker run --rm -p 4444:4444 -p 5900:5900 \
-v /tmp/chrome_profiles:/tmp/chrome_profiles \
-e JAVA_OPTS selenium/standalone-chrome:latest
or
# To execute this docker-compose yml file use `docker-compose -f <file_name> up`
# Add the `-d` flag at the end for detached execution
version: '2'
services:
chrome:
image: selenium/node-chrome:latest
volumes:
- /dev/shm:/dev/shm
- /tmp/chrome:/tmp/chrome_profiles
ports:
- "5900:5900"
depends_on:
- hub
environment:
HUB_HOST: hub
hub:
image: selenium/hub:latest
ports:
- "4444:4444"
然后需要通过 ChromeOptions 传递配置文件路径,请记住这是容器内的路径。示例代码:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("-profile", "/tmp/chrome_profiles/<replace_with_profile_id>.selenium");
WebDriver driver = new RemoteWebDriver(new URL("http://hub:4444/wd/hub"), chromeOptions);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)