Python 中的 Selenium 线选项

问题描述 投票:0回答:2

所以我在 Python 中使用 Selenium Wire 来浏览网站,目前我的代码失败并出现以下错误

错误:ssl_client_socket_impl.cc(959)]握手失败;返回 -1,SSL 错误代码 1,net_error -100'''

当我收到此错误时,Selenium 似乎与互联网断开连接,因此后续的点击和交互不起作用。我在网上查了一下,明白我需要传递以下参数(有道理,但如果我错了请纠正我)

options.add_argument('--ignore-certificate-errors-spki-list')
options.add_argument('--ignore-ssl-errors').

我已经有以下使用代理服务器的代码,但我不确定如何将上述参数传递到我当前的 Selenium 选项中,并且代理选项已经就位。我希望这是有道理的?!

(出于安全原因,我更改了我的代理服务器详细信息 obvs)。

谢谢!!!!!!

    import selenium

    from selenium.webdriver.common.keys import Keys

    from selenium.webdriver.common.by import By


    url = 'http://www.whatsmyipaddress.com'

    from seleniumwire import webdriver


    options = {
        'proxy': {
            'http': 'http://myusername:[email protected]:123456', 
            'https': 'http://myusername:[email protected]:123456',
            'no_proxy': 'localhost,127.0.0.1' # excludes
        }  
    }

    driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe", 
    seleniumwire_options=options)

    driver.get(url=url)
python selenium selenium-webdriver
2个回答
16
投票

您应该使用

seleniumwire_options
arg(
seleniumwire.webdriver.Chrome
)作为代理选项,并使用
options
arg 作为其余选项。

代码示例:

    from seleniumwire import webdriver

    # selenium-wire proxy settings
    # note: setting https:// for the 'http' key as well is not a mistake,
    # but a workaround to avoid `ValueError: 
    # Different settings for http and https proxy servers not supported`
    seleniumwire_options = {
            'proxy': {
                'https': 'https://myusername:[email protected]:123456', 
                'https': 'https://myusername:[email protected]:123456',
                'no_proxy': 'localhost,127.0.0.1' # excludes
            }  
        }
    
    # other Chrome options
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--ignore-certificate-errors-spki-list')
    chrome_options.add_argument('--ignore-ssl-errors')
    
    driver = webdriver.Chrome('chromedriver', 
                              options=chrome_options,
                              seleniumwire_options=seleniumwire_options)

0
投票

Botasaurus 框架支持 SSL,其经过身份验证的代理 sych 为 http://username:password@proxy-provider-domain:port。

seleniumwire-vs-botasaurus

安装

pip install botasaurus

示例

from botasaurus import *

@browser(proxy="http://username:password@proxy-provider-domain:port") # TODO: Replace with your own proxy 
def visit_ipinfo(driver: AntiDetectDriver, data):
    driver.get("https://ipinfo.io/")
    driver.prompt()

visit_ipinfo()

您可以了解关于Botasaurus 这里

© www.soinside.com 2019 - 2024. All rights reserved.