2012-07-12 28 views
16

我使用這個代碼:我如何在Python設置代理鍍鉻的webdriver

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設置代理爲FF。這適用於FF。如何在Chrome中設置這樣的代理?我發現這個exmaple但不是很有幫助。當我運行腳本沒有任何反應(Chrome瀏覽器未啓動)。

+0

不好意思問明顯,但你改變'Firefox'線自己的Chrome等價物?你能發佈你的代碼嗎? – 2012-07-12 10:58:07

+0

另外,你的意思是「什麼都沒有發生?」是否有錯誤訊息?任何類型的退出狀態? – 2012-07-12 10:58:38

+0

我注意到,如果我在Internet Explorer中設置了代理,那麼腳本無法正常工作(FF正在打開,但在driver.get(「google.com/」;))上失敗。沒有錯誤信息,它拒絕連接。如果在Internet Explorer中沒有啓用代理設置,該腳本正在工作。 – sarbo 2012-07-12 11:55:36

回答

35
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(chrome_options=chrome_options) 
chrome.get("http://whatismyipaddress.com") 
+1

有沒有沒有重新啓動瀏覽器的方法? thx – 176coding 2016-10-05 03:07:15

-2
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') 
+3

不回答問題。 – Collin 2014-07-15 15:32:50

+1

鉻 - 問題是關於Chrome – 2017-11-02 14:31:51

6

它的工作對我來說...

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") 
+0

如果代理需要sauthen,你如何添加用戶名/密碼? – desmond 2018-03-03 19:16:37

4

我有同樣的事情的問題。 ChromeOptions非常奇怪,因爲它沒有像您想象的那樣與所需的功能集成在一起。我忘記了確切的細節,但基本上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」kwarg。現在,我只是這樣做是爲了把我自己的字典:

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)