为什么从请求切换到 selenium 时代理不起作用?

问题描述

我在 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")

这是自动浏览器会话屏幕截图

enter image description here

我的 selenium 配置有问题吗?

谢谢

解决方法

我遇到了类似的问题切换Firefox 驱动程序解决了这个问题。

如果你想坚持使用 chrome,也许你可以尝试这种方法:

    options = webdriver.ChromeOptions()
    options.add_argument(f'--proxy-server={PROXY}')
    driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...