2012-07-12 13 views
16

अजगर में क्रोम के लिए प्रॉक्सी सेट मैं इस कोड का उपयोग कर रहा है कि कैसे कार्य करें:मैं 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) 

अजगर webdriver में एफएफ के लिए प्रॉक्सी सेट करने के। यह एफएफ के लिए काम करता है। क्रोम में इस तरह प्रॉक्सी कैसे सेट करें? मुझे यह exmaple मिला लेकिन यह बहुत उपयोगी नहीं है। जब मैं स्क्रिप्ट चलाता हूं तो कुछ नहीं होता (क्रोम ब्राउज़र शुरू नहीं होता है)।

+0

स्पष्ट पूछने के लिए खेद है, लेकिन क्या आपने 'फ़ायरफ़ॉक्स' लाइनों को उनके क्रोम समकक्षों में बदल दिया है? क्या आप अपना कोड पोस्ट कर सकते हैं? –

+0

इसके अलावा, आपका क्या मतलब है "कुछ भी नहीं होता?" क्या कोई त्रुटि संदेश है? किसी भी प्रकार की निकास स्थिति? –

+0

मैंने देखा कि यदि मेरे पास इंटरनेट एक्सप्लोरर में प्रॉक्सी सेट है तो स्क्रिप्ट काम नहीं कर रही है (एफएफ खुल रहा है लेकिन driver.get ("google.com/";) पर विफल रहता है। कोई त्रुटि संदेश नहीं है, यह कनेक्ट करने से इंकार कर देता है। यदि इंटरनेट एक्सप्लोरर में कोई प्रॉक्सी सेटिंग्स सक्षम नहीं है तो स्क्रिप्ट काम कर रही है। – 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

क्रोम - प्रश्न क्रोम –

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

4

मैं एक ही बात के साथ एक समस्या हुई। क्रोमऑप्शन बहुत अजीब है क्योंकि यह वांछित क्षमताओं के साथ एकीकृत नहीं है जैसा आप सोचेंगे। मैं सटीक विवरण भूल जाता हूं, लेकिन मूल रूप से 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 था। अब मैं बस अपना खुद का निर्देश निर्धारित करने के लिए ऐसा करता हूं:

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)