如何在Python WebDriver中为Chrome设置代理?

64

我正在使用以下代码:

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "proxy.server.address")
profile.set_preference("network.proxy.http_port", "port_number")
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)

如何在Python WebDriver中设置Chrome代理?这段代码可以在Firefox中运行,但是对于Chrome来说该怎么设置代理呢?我找到了这个例子,但并没有太大帮助。当我运行脚本时,没有任何反应(Chrome浏览器未启动)。


很抱歉问题有点简单,但您是否将Firefox的代码改成了相应的Chrome代码?您能否发布您的代码? - David Cain
1
另外,你所说的“什么都没发生”是什么意思?有错误信息吗?或者有任何退出状态吗? - David Cain
我注意到,如果在Internet Explorer中设置了代理,脚本将无法工作(FF会打开但在driver.get("google.com/";)上失败)。没有错误消息,它拒绝连接。如果在Internet Explorer中没有启用代理设置,则该脚本可以正常工作。 - sarbo
7个回答

116
from selenium import webdriver

PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)

chrome = webdriver.Chrome(options=chrome_options)
chrome.get("http://whatismyipaddress.com")

2
错误: WebDriverException: 消息:'chromedriver' 可执行文件需要在 PATH 中。请参见 https://sites.google.com/a/chromium.org/chromedriver/home - Kishan Mehta
3
这个例子能否加入用户名/密码代理身份验证? - Liquidgenius
我也卡在这里了@Liquidgenius。你能帮我吗? - Sajjad Zaidi
@SajjadZaidi 抱歉已经有一段时间了,我不记得我是如何解决这个问题的。 - Liquidgenius
请查看下面的评论以获取有关用户名/密码问题的答案。 - Redithion

17

对我来说它正在工作...

from selenium import webdriver

PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s' % PROXY)

chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")

14
如果代理需要身份验证,你该如何添加用户名和密码? - desmond
我想你会在参数中传递用户名和密码,然后查找机器人的参数键并按需填充。 - scriptso
10
在URL中添加它们:scheme://user:pass@my.great.host:port。其中,scheme代表协议,user和pass分别表示用户名和密码,my.great.host表示主机名,port表示端口号。 - Nate Symer

8

我曾遇到同样的问题。ChromeOptions很奇怪,因为它并没有像你想象的那样与desiredcapabilities集成。我忘了具体的细节,但基本上,根据你是否传递了一个desired capabilities字典,ChromeOptions将重置某些值为默认值。

我进行了以下猴子补丁,使我能够指定自己的字典,而不用担心ChromeOptions的复杂性。

更改 /selenium/webdriver/chrome/webdriver.py 中的以下代码:

def __init__(self, executable_path="chromedriver", port=0,
             chrome_options=None, service_args=None,
             desired_capabilities=None, service_log_path=None, skip_capabilities_update=False):
    """
    Creates a new instance of the chrome driver.

    Starts the service and then creates new instance of chrome driver.

    :Args:
     - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
     - port - port you would like the service to run, if left as 0, a free port will be found.
     - desired_capabilities: Dictionary object with non-browser specific
       capabilities only, such as "proxy" or "loggingPref".
     - chrome_options: this takes an instance of ChromeOptions
    """
    if chrome_options is None:
        options = Options()
    else:
        options = chrome_options

    if skip_capabilities_update:
        pass
    elif desired_capabilities is not None:
        desired_capabilities.update(options.to_capabilities())
    else:
        desired_capabilities = options.to_capabilities()

    self.service = Service(executable_path, port=port,
        service_args=service_args, log_path=service_log_path)
    self.service.start()

    try:
        RemoteWebDriver.__init__(self,
            command_executor=self.service.service_url,
            desired_capabilities=desired_capabilities)
    except:
        self.quit()
        raise 
    self._is_remote = False

所有改变的仅是 "skip_capabilities_update" 关键字参数。现在我只需这样设置自己的字典:

capabilities = dict( DesiredCapabilities.CHROME )

if not "chromeOptions" in capabilities:
    capabilities['chromeOptions'] = {
        'args' : [],
        'binary' : "",
        'extensions' : [],
        'prefs' : {}
    }

capabilities['proxy'] = {
    'httpProxy' : "%s:%i" %(proxy_address, proxy_port),
    'ftpProxy' : "%s:%i" %(proxy_address, proxy_port),
    'sslProxy' : "%s:%i" %(proxy_address, proxy_port),
    'noProxy' : None,
    'proxyType' : "MANUAL",
    'class' : "org.openqa.selenium.Proxy",
    'autodetect' : False
}

driver = webdriver.Chrome( executable_path="path_to_chrome", desired_capabilities=capabilities, skip_capabilities_update=True )

7

很容易!

首先,定义您的代理网址。

proxy_url = "127.0.0.1:9009"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_url,
    'sslProxy': proxy_url,
    'noProxy': ''})

然后,创建Chrome的能力设置并将代理添加到其中。

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

最后,创建WebDriver并传递所需的功能

driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")

总的来说,它看起来是这样的

from selenium import webdriver
from selenium.webdriver.common.proxy import *

proxy_url = "127.0.0.1:9009"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_url,
    'sslProxy': proxy_url,
    'noProxy': ''})

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")

6
这对我非常有效:

像魔术一样起作用:

proxy = "localhost:8080"
desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()
desired_capabilities['proxy'] = {
    "httpProxy": proxy,
    "ftpProxy": proxy,
    "sslProxy": proxy,
    "noProxy": None,
    "proxyType": "MANUAL",
    "class": "org.openqa.selenium.Proxy",
    "autodetect": False
}

3
登录凭证在哪里? - Tajs

5

对于那些想在需要身份验证的Chrome中设置代理服务器的人,应该遵循以下步骤。

  1. 在您的项目中创建一个proxy.py文件,使用code,并每次需要时调用proxy_chrome。您需要传递代理服务器、端口和身份验证的用户名密码等参数。
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import zipfile,os

def proxy_chrome(PROXY_HOST,PROXY_PORT,PROXY_USER,PROXY_PASS):
    manifest_json = """
            {
                "version": "1.0.0",
                "manifest_version": 2,
                "name": "Chrome Proxy",
                "permissions": [
                    "proxy",
                    "tabs",
                    "unlimitedStorage",
                    "storage",
                    "<all_urls>",
                    "webRequest",
                    "webRequestBlocking"
                ],
                "background": {
                    "scripts": ["background.js"]
                },
                "minimum_chrome_version":"22.0.0"
            }
            """

    background_js = """
    var config = {
            mode: "fixed_servers",
            rules: {
              singleProxy: {
                scheme: "https",
                host: "%(host)s",
                port: parseInt(%(port)d)
              },
              bypassList: ["foobar.com"]
            }
          };
    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
    function callbackFn(details) {
        return {
            authCredentials: {
                username: "%(user)s",
                password: "%(pass)s"
            }
        };
    }
    chrome.webRequest.onAuthRequired.addListener(
                callbackFn,
                {urls: ["<all_urls>"]},
                ['blocking']
    );
        """ % {
            "host": PROXY_HOST,
            "port": PROXY_PORT,
            "user": PROXY_USER,
            "pass": PROXY_PASS,
        }


    pluginfile = 'extension/proxy_auth_plugin.zip'

    with zipfile.ZipFile(pluginfile, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

    co = Options()
    #extension support is not possible in incognito mode for now
    #co.add_argument('--incognito')
    co.add_argument('--disable-gpu')
    #disable infobars
    co.add_argument('--disable-infobars')
    co.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
    #location of chromedriver, please change it according to your project.
    chromedriver = os.getcwd()+'/Chromedriver/chromedriver'
    co.add_extension(pluginfile)
    driver = webdriver.Chrome(chromedriver,chrome_options=co)
    #return the driver with added proxy configuration.
    return driver

太棒了,那是唯一一个对我有效的解决方案。 - user1050755
对于可能遇到和我一样问题的其他人,这个解决方案之所以不起作用,是因为我需要将方案从"https"更改为"http"。在那之后就可以正常工作了 :) - undefined

-14
from selenium import webdriver
from selenium.webdriver.common.proxy import *

myProxy = "86.111.144.194:3128"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy':''})

driver = webdriver.Firefox(proxy=proxy)
driver.set_page_load_timeout(30)
driver.get('http://whatismyip.com')

7
未回答问题。 - Talia
4
当谈论Chrome时,这是关于Chrome浏览器的问题。 - Dave Lawrence
1
Chrome的答案 - rishabh singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接