如何为 selenium 设置 socks4 代理?

How can I set socks4 proxy for selenium?

我需要设置袜子版本 4,但我进行了很多搜索,但找不到我的答案。 最后我写了这段代码,但我得到了错误

prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.autodetect = False
prox.socks_proxy = '<ip>:<port>'
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
browser = webdriver.Chrome(executable_path=chrome_driver_path, options=chrome_options, desired_capabilities=capabilities)
browser.get('https://www.google.com')

我的错误

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: proxy
from invalid argument: Specifying 'socksProxy' requires an integer for 'socksVersion'
PROXY = "127.0.0.1:9150"
webdriver.DesiredCapabilities.CHROME['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "proxyType": "MANUAL",
    "socksProxy": PROXY,
    "socksVersion":5

}

您可以使用所需的功能,您不必传递它,因为您正在直接更新 chrome 的功能

否则你也可以使用

PROXY = "127.0.0.1:9150"
caps= webdriver.DesiredCapabilities.CHROME.copy()
caps['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "proxyType": "MANUAL",
    "socksProxy": PROXY,
    "socksVersion":5

}
driver = webdriver.Chrome(desired_capabilities=caps)