硒铬Firefox WebDriver:在Python中设置HTTPS代理 [英] Selenium Chrome & Firefox WebDriver: Set HTTPS proxy in Python

查看:114
本文介绍了硒铬Firefox WebDriver:在Python中设置HTTPS代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了大量的搜索工作,但是那里有很多非常相似的令人困惑的片段.

I've done plenty of searching however there is a lot of confusing snippets out there that are very similar.

我尝试使用 DesiredCapabilities ChromeOptions Options 和一系列参数,但是没有任何效果:(设置代理.

I've attempted to use the DesiredCapabilities, ChromeOptions, Options and a series of arguments but nothing is working :( It fails to set a proxy.

例如( ChromeOptions )

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy=https://' + proxy_ip_and_port)
chrome_options.add_argument('--proxy-auth=' + proxy_user_and_pass)
chrome_options.add_argument('--proxy-type=https')

browser = webdriver.Chrome("C:\drivers\chromedriver.exe")

另一个示例( Options )

    options = Options()
    options.add_argument('--proxy=https://' + proxy_ip_and_port)
    options.add_argument('--proxy-auth=' + proxy_user_and_pass)
    options.add_argument('--proxy-type=https')

    browser = webdriver.Chrome("C:\drivers\chromedriver.exe", chrome_options=options)

我还使用了-proxy-server 而不是-proxy-auth -proxy-type ...甚至采用以下格式:'--proxy-server = http://'+ proxy_user_and_pass +'@'+ proxy_ip_and_port

I've also used --proxy-server instead of --proxy-auth, --proxy-type... etc even in the format of: '--proxy-server=http://' + proxy_user_and_pass + '@' + proxy_ip_and_port

另一个示例( DesiredCapabilities )

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {'proxyType': 'MANUAL',
                         'httpProxy': proxy_ip_and_port,
                         'ftpProxy': proxy_ip_and_port,
                         'sslProxy': proxy_ip_and_port,
                         'noProxy': '',
                         'class': "org.openqa.selenium.Proxy",
                         'autodetect': False}

capabilities['proxy']['socksUsername'] = proxy_user
capabilities['proxy']['socksPassword'] = proxy_pass

browser = webdriver.Chrome(executable_path="C:\drivers\chromedriver.exe", desired_capabilities=capabilities)

我也曾在Firefox中尝试过,但同样的问题也会发生,它使用的浏览器使用的是我的普通IP.

I've tried in Firefox too but the same issue happens, it uses the browser with my normal IP.

推荐答案

根据最新文档(2020年7月),您为 FIREFOX CHROME 设置了 DesiredCapabilities .

According to the latest documentation (Jul 2020) you set the DesiredCapabilities for either FIREFOX or CHROME.

我已经在 Firefox 上对其进行了测试.之后,您可以检查浏览器的连接设置以验证代理设置正确.

I've tested it for Firefox. You can check your browser's connection settings afterwards to validate the proxy was set correctly.

from selenium import webdriver

PROXY = "<HOST>:<PORT>"  # HOST can be IP or name
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,    # this is the https proxy
    "proxyType": "MANUAL",
}

with webdriver.Firefox() as driver:
    # Open URL
    driver.get("https://selenium.dev")

Selenium Wiki .您将在此处看到属性 sslProxy https 设置代理.

The proxy-dict itself is documented in the Selenium wiki. You'll see here that the attribute sslProxy sets the proxy for https.

我还没有为 Chrome 测试过它.如果不行,您可以在Google的 ChromeDriver文档.根据这一点,您还需要使用 desired_capabilites 参数实例化webdriver(这实际上与您的示例非常相似,因此,相比经过验证的解决方案,这更像是一个猜测):

I haven't tested it for Chrome though. If it shouldn't work, you may find clues in Google's ChromeDriver documentation. According to this you also need to instantiate the webdriver with the desired_capabilites parameter (which is then actually very similar to your example, so this is now more of a guess than a proven solution):

caps = webdriver.DesiredCapabilities.CHROME.copy()
caps['proxy'] = ... # like described above
driver = webdriver.Chrome(desired_capabilities=caps)
driver.get("https://selenium.dev")

这篇关于硒铬Firefox WebDriver:在Python中设置HTTPS代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆