【问题标题】:Python3, can't set socks proxy with chromedriver (socks.py type integer error)Python3,无法使用 chromedriver 设置 socks 代理(socks.py 类型整数错误)
【发布时间】:2018-06-03 03:03:56
【问题描述】:

我正在尝试通过带有 chromedriver 和 python3.5 的 localhost 使用 socks5 代理。但是,我收到以下错误:

loading
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    browser = webdriver.Chrome(chrome_options=options)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 97, in start
    if self.is_connectable():
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 113, in is_connectable
    return utils.is_connectable(self.port)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/utils.py", line 106, in is_connectable
    socket_ = socket.create_connection((host, port), 1)
  File "/usr/lib/python3.5/socket.py", line 702, in create_connection
    sock.connect(sa)
  File "/usr/local/lib/python3.5/dist-packages/socks.py", line 766, in connect
    _BaseSocket.connect(self, proxy_addr)
TypeError: an integer is required (got type str)

这是我正在使用的代码。

import time
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=socks5://127.0.0.1:9000")

print("loading")
browser = webdriver.Chrome(chrome_options=options)
print("getting url")
browser.get("http://www.atagar.com/echo.php")

这是 webdriver.py 中的错误吗?它如何解析选项并将它们发送到绑定到代理?还是我在这里做错了什么?

【问题讨论】:

  • 我现在在尝试使用 Firefox 和 geckdriver 而不是 chromedriver 时确认了同样的问题。开始感觉像是 webdriver 中的错误或版本问题。谁能确认一下?

标签: python proxy selenium-chromedriver


【解决方案1】:

遇到了类似的问题。这就是我最终让它工作的方式。

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

options = webdriver.ChromeOptions()

proxy = Proxy()
proxy.proxyType = ProxyType.MANUAL
proxy.autodetect = False
proxy.httpProxy = proxy.sslProxy = proxy.socksProxy = "127.0.0.1:9000"
options.Proxy = proxy
options.add_argument("ignore-certificate-errors")


driver = webdriver.Chrome('/Users/benjamin/Developer/chromedriver', 
                          chrome_options=options) 

【讨论】:

  • 赞成。此外, chrome_options 已被弃用。所以你想改用选项。
猜你喜欢
  • 2018-12-06
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 2018-12-22
  • 1970-01-01
  • 2014-10-16
相关资源
最近更新 更多