2012-07-12 303 views
16

webdriverをpythonでクロムのプロキシを設定します。これはFFで動作します。 Chromeでこのようなプロキシを設定するにはどうすればよいですか?私はこれを見つけたexmapleしかし非常に有用ではない。スクリプトを実行すると何も起こりません(Chromeブラウザは起動していません)。は、どのように私はこのコードを使用してい

+0

私は申し訳ありませんが、あなたは 'Chrome'の行をChromeの同等のものに変更しましたか?コードを投稿できますか? –

+0

また、「何も起こりません」という意味はどうですか?エラーメッセージはありますか?どんな種類の終了ステータスですか? –

+0

Internet Explorerでプロキシが設定されていると、スクリプトが動作していないことがわかりました(FFは開かれていますが、driver.get( "google.com/";)では失敗します)。エラーメッセージはなく、接続を拒否します。 Internet Explorerでプロキシ設定が有効になっていない場合、スクリプトは動作しています。 – sarbo

答えて

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

-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

+1

Chrome - Chromeに関する質問 –

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

プロキシが必要な場合は、どのようにユーザー名/パスワードを追加しますか? – desmond

4

私は同じことで問題がありました。 ChromeOptionsは非常に奇妙です。なぜなら、あなたが思うような希望の機能と統合されていないからです。正確な詳細は忘れていますが、基本的にChromeOptionsは、希望する機能を実行したかどうかに基づいてデフォルト値にリセットされます。

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 

は、私は私がChromeOptions

の合併症について

変更/selenium/webdriver/chrome/webdriver.pyに次のコードを気にせず自分の辞書を指定することができ、次のサルのパッチをしました

「skip_capabilities_update」kwargが変更されました。今私はちょうど私自身のdictを設定するためにこれを行います:

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) 
関連する問題