2012-07-12 8 views
16

webdriver 파이썬에서 크롬에 대한 프록시를 설정합니다. 이것은 FF에서 작동합니다. Chrome에서 이와 같은 프록시를 설정하는 방법은 무엇인가요? 이 exmaple을 찾았지만별로 도움이되지 않습니다. 스크립트를 실행할 때 아무 일도 일어나지 않습니다 (Chrome 브라우저가 시작되지 않음). 나를 위해 일하는는 어떻게 내가이 코드를 사용하고

+0

미안하지만 'Firefox' 라인을 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에 대한 질문이 –

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에 다음 코드를 걱정하지 않고 내 자신의 DICT을 지정할 수 있습니다 다음 원숭이 패치를했다

변경된 사항은 모두 "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) 
관련 문제