问题描述
我在 Stackoverflow 上尝试了其他解决方案,但没有一个对我有用。
我正在尝试使用 代理 配置 selenium,它与 requests 库一起使用,我使用了以下命令:
requests.get('https://stackoverflow.com',proxies={'https':'https://3.0.32.21:22881'})
<Response [200]>
但是当我使用 Selenium 尝试相同的 代理 时,它根本不起作用,这是我尝试的关于 Selenium 配置的方法使用代理:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
PROXY = "3.0.32.21:22881"
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['goog:loggingPrefs'] = {'performance': 'ALL'}
capabilities['proxy'] = {
"httpProxy": PROXY,"ftpProxy": PROXY,"sslProxy": PROXY,"noproxy": None,"proxyType": "MANUAL","class": "org.openqa.selenium.Proxy","autodetect": False}
driver = webdriver.Chrome('chromedriver',desired_capabilities=capabilities)
driver.get("https://stackoverflow.com")
这是自动浏览器会话的屏幕截图:
我的 selenium 配置有问题吗?
谢谢
解决方法
我遇到了类似的问题切换到 Firefox 驱动程序解决了这个问题。
如果你想坚持使用 chrome,也许你可以尝试这种方法:
options = webdriver.ChromeOptions()
options.add_argument(f'--proxy-server={PROXY}')
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)