我正在使用 browsermob 的 python 客户端来记录我的 selenium 测试的流量。 Selenium 网格位于带有 chrome 和 firefox 图像的 docker 容器中。我似乎无法正确配置 docker 图像以连接到代理和网格。 这是我用来创建代理和远程 Web 驱动程序的代码:
server = browsermobproxy.Server('mylocalpathtobrowsermobbin')
server.start()
proxy = server.create_proxy()
proxy.new_har()
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities={
'browserName': 'chrome',
'chromeOptions': {
'args': ["--proxy-server={}".format(proxy.proxy)]}
})
这是我的 docker-compose 文件:
hub: image: selenium/hub ports: - "4444:4444" chrome: image: selenium/node-chrome-debug volumes: - /dev/shm:/dev/shm links: - hub ports: - "5900:5900"
我是 docker 的新手,我知道我需要公开代理用于连接的端口,但我无法让它工作。感谢任何帮助,谢谢!
最佳答案
根据 Sergey 的回复回答我自己的问题: 我将 browsermob-proxy 图像推送到 docker hub: https://hub.docker.com/r/spothero/browsermob-proxy/
从这个存储库创建: https://github.com/sskorol/docker-browsermob-proxy
所有功劳都归功于 Sergey 的 docker 文件。
我的 docker compose 文件:
hub: image: selenium/hub ports: - "4444:4444" firefox: image: selenium/node-firefox links: - hub chrome: image: selenium/node-chrome-debug volumes: - /dev/shm:/dev/shm links: - hub ports: - "5900:5900" browsermobproxy: image: spothero/browsermob-proxy:1.0.0 ports: - "9090-9191:9090-9191" expose: - "9090-9191" links: - hub - firefox - chrome
On the jenkins job I have a shell step:
#!/bin/bash docker-compose up -d --force-recreate sleep 10s PROXY_IP_ADDRESS="$(docker inspect --format {{.NetworkSettings.IPAddress}} browsermobproxy_1)" export BROWSERMOB_CONTAINER_HOST=$PROXY_IP_ADDRESS
I use an environment variable to pass the hosts to my test code. Here is the code that initializes the webdriver with the proxy:
import browsermobproxy
self.client = browsermobproxy.Client('localhost:9090')
self.driver = webdriver.Remote(
command_executor=settings.SELENIUM_GRID_HUB,
desired_capabilities={
'browserName': 'chrome',
'chromeOptions': {
'args': ["--proxy-server={}".format(
os.environ.get('BROWSERMOB_CONTAINER_HOST'), self.client.port)]
}
})
希望这对您有所帮助!
关于python - 在 docker 上使用 selenium 网格运行 browsermob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007957/