Selenium webdriver.Remote 驱动程序不适用于 Tor 代理(webdriver.Chrome 可以)

Selenium webdriver.Remote driver does not work with tor proxy(webdriver.Chrome does)(Selenium webdriver.Remote 驱动程序不适用于 Tor 代理(webdriver.Chrome 可以))

本文介绍了Selenium webdriver.Remote 驱动程序不适用于 Tor 代理(webdriver.Chrome 可以)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 远程驱动程序上使用 socks5 代理,它作为docker 容器在 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()

这里是 selenium hub 如何与 tor 代理.您可以在 docker 中创建一个网络,将容器附加到它,然后使用容器名称作为代理主机:

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",关于":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

这篇关于Selenium webdriver.Remote 驱动程序不适用于 Tor 代理(webdriver.Chrome 可以)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Selenium webdriver.Remote 驱动程序不适用于 Tor 代理(webdriver.Chrome 可以)

基础教程推荐