Selenium webdriver.Remote驱动程序不适用于Tor代理(webdriver.Chrome可以)
我正在尝试在作为主机托管的远程驱动程序上使用socks5代理端口4444上的docker容器.
I'm trying to use socks5 proxy on my remote driver which is hosted as a docker container on port 4444.
这是代码示例:
from selenium import webdriver
opts = webdriver.ChromeOptions()
opts.add_argument("--no-sandbox")
opts.add_argument("--disable-dev-shm-usage")
opts.add_argument("--proxy-server=socks5://127.0.0.1:9050")
driver = webdriver.Remote(command_executor="http://localhost:4444/wd/hub", desired_capabilities=opts.to_capabilities())
然后,当我尝试打开任何页面时,出现错误,提示检查您的代理设置或与您的网络管理员联系
.
Then, when I try to open any page, I get error stating Check your proxy settings or contact your network administrator
.
虽然在常规代理上使用相同的代码示例–效果很好.当我通过 9050
端口进行粗体请求时-可以正常工作.
While using same code sample on regular proxy – it works just fine.
When I do bold request through 9050
port – it works just fine.
最后,当我将相同的代码示例与 webdriver.Chrome
而不是 webdriver.Remote
一起使用时,效果很好!
And finally, when I use same code sample with webdriver.Chrome
instead of webdriver.Remote
it works fine!
我希望通过远程Webdriver使一切正常的任何建议.
I would appreciate any suggestions to make things work through Remote webdriver.
更新:我正在使用 selenium == 3.14.0
,RemoteDriver正在获取docker镜像 selenium/node-chrome-debug:3.141.59-radium
.
UPDATE:
I'm using selenium==3.14.0
and RemoteDriver is getting docker image selenium/node-chrome-debug:3.141.59-radium
.
对于macOS和Windows,您可以使用 host.docker.internal
从容器访问本地主机:
For macOS and Windows you can use host.docker.internal
to access local host from container:
from selenium import webdriver
opts = webdriver.ChromeOptions()
opts.add_argument("--no-sandbox")
opts.add_argument("--disable-dev-shm-usage")
opts.add_argument("--proxy-server=socks5://host.docker.internal:9050")
driver = webdriver.Remote(command_executor="http://127.0.0.1:4444/wd/hub", desired_capabilities=opts.to_capabilities())
driver.get("http://jsonip.com/")
print(driver.find_element_by_css_selector("html").text)
driver.quit()
以下是硒中心与
Here is how selenium hub works with tor proxy. You can create a network in docker, attach containers to it and then use container name as a proxy host:
docker network create mynetwork
docker run -it -p 8118:8118 -p 9050:9050 --name tor-proxy -d dperson/torproxy
docker run -d -p 4444:4444 --name selenium-hub -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-yttrium
docker network connect mynetwork hub
docker network connect mynetwork tor-proxy
这里是相同的示例,但使用docker-compose:
Here is the same example but with docker-compose:
version: '3.5'
services:
tor-proxy:
image: dperson/torproxy
container_name: tor-proxy
ports:
- "8118:8118"
- "9050:9050"
networks:
- mynetwork
selenium-hub:
image: selenium/standalone-chrome:3.141.59-yttrium
container_name: selenium-hub
ports:
- "4444:4444"
networks:
- mynetwork
networks:
mynetwork:
name: mynetwork
driver: bridge
Python代码:
from selenium import webdriver
opts = webdriver.ChromeOptions()
opts.add_argument("--no-sandbox")
opts.add_argument("--disable-dev-shm-usage")
opts.add_argument("--proxy-server=socks5://tor-proxy:9050")
driver = webdriver.Remote(command_executor="http://127.0.0.1:4444/wd/hub", desired_capabilities=opts.to_capabilities())
driver.get("http://jsonip.com/")
print(driver.find_element_by_css_selector("html").text)
driver.quit()
结果:
{"ip":"18.27.197.252","about":"https://jsonip.com/about","Pro!":"http://getjsonip.com",获取"通知":"https://jsonip.com/notify"}
{"ip":"18.27.197.252","about":"https://jsonip.com/about","Pro!":"http://getjsonip.com","Get Notifications": "https://jsonip.com/notify"}
以退出代码0结束的过程
Process finished with exit code 0
使用更改后的IP重新运行:
Run again with changed IP:
{"ip":"178.165.72.177","about":"https://jsonip.com/about","Pro!":"http://getjsonip.com",获取"通知":"https://jsonip.com/notify"}
{"ip":"178.165.72.177","about":"https://jsonip.com/about","Pro!":"http://getjsonip.com","Get Notifications": "https://jsonip.com/notify"}
以退出代码0结束的过程
Process finished with exit code 0