问题
I'm trying to use a socks5 proxy via localhost with chromedriver and python3.5. However, I get the following error:
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)
Here's the code I'm using.
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")
Is this a bug in webdriver.py and how it parses the options and sends them to bind to the proxy? Or is there something I am doing wrong here?
回答1:
Had a similar problem. This is how i got it working eventually.
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)
来源:https://stackoverflow.com/questions/50662935/python3-cant-set-socks-proxy-with-chromedriver-socks-py-type-integer-error