Python3,无法使用chromedriver设置socks代理(socks.py类型整数错误)

时间:2018-06-03 03:03:57

标签: python proxy selenium-chromedriver

我试图通过localhost使用带有chromedriver和python3.5的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中的错误以及它如何解析选项并将它们发送到绑定到代理?或者我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

有类似的问题。这就是我最终使它工作的方式。

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)