2012-01-16 125 views
14

我想用密码保护的代理来使用selenium。代理不是固定的,而是一个变量。所以这个必须在代码中完成(只需在这台特定的机器上设置firefox来处理代理是不太理想的)。到目前为止,我有以下代码:使用Python的Selenium:输入/提供Firefox的http代理密码

fp = webdriver.FirefoxProfile() 
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5 
fp.set_preference("network.proxy.type", 1) 

fp.set_preference("network.proxy.http", PROXY_HOST) 
fp.set_preference("network.proxy.http_port", PROXY_PORT) 

driver = webdriver.Firefox(firefox_profile=fp) 
driver.get("http://whatismyip.com") 

在这一点上,弹出对话框,请求代理用户/通行证。

是否有到任何一个简单的方法:

  1. 键入用户/对话框通过。
  2. 在早期阶段提供用户/通行证。
+2

嘿队友,你有没有找到解决办法呢? – Shane 2012-03-03 12:47:34

+1

@Shane你找到解决方案了吗? – 2015-09-11 10:44:57

+2

@ArsenIbragimov你找到解决方案了吗? – Math 2015-10-29 20:08:24

回答

-4

你试过PROXY_HOST = "http://username:[email protected]"

Also

Starting with Selenium 2.0 beta 1, there is built in support for handling popup dialog boxes.

+2

'PROXY_HOST =“username:[email protected]”'不起作用。至于弹出对话框处理,我如何找到用户/传递元素来输入? – Shane 2012-03-03 12:33:49

+0

@Shane,你试过'PROXY_HOST =“username:[email protected]”'或'PROXY_HOST =“http:// username:[email protected]”'。第二个应该通过基本身份验证。关于元素位置:如果你可以切换到对话框,你总是可以浏览其发送'tab'键信号的元素。 – 2012-03-06 04:21:31

8

硒能不能做到这一点本身。我发现有用的唯一方法是here。简而言之,您需要添加一个浏览器扩展以进行身份​​验证。这看起来要简单得多。

下面是它的工作原理为Chrome(对我来说):

  1. 创建一个zip文件proxy.zip包含两个文件:

background.js

var config = { 
    mode: "fixed_servers", 
    rules: { 
     singleProxy: { 
     scheme: "http", 
     host: "YOU_PROXY_ADDRESS", 
     port: parseInt(YOUR_PROXY_PORT) 
     }, 
     bypassList: ["foobar.com"] 
    } 
    }; 

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); 

function callbackFn(details) { 
    return { 
     authCredentials: { 
      username: "YOUR_PROXY_USERNAME", 
      password: "YOUR_PROXY_PASSWORD" 
     } 
    }; 
} 

chrome.webRequest.onAuthRequired.addListener(
     callbackFn, 
     {urls: ["<all_urls>"]}, 
     ['blocking'] 
); 

别忘了更换YOUR_PROXY_ *到您的设置。

的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" 
} 
  • 添加创建proxy.zip作为扩展

    from selenium import webdriver 
    from selenium.webdriver.chrome.options import Options 
    
    chrome_options = Options() 
    chrome_options.add_extension("proxy.zip") 
    
    driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options) 
    driver.get("http://google.com") 
    driver.close() 
    
  • 就是这样。对我来说,这就像一个魅力。如果您需要动态创建proxy.zip或需要PHP示例,请转至original post

    +0

    真棒!!!!!锻炼了魅力!干杯迈克 – 2016-03-07 11:41:49

    +0

    迈克,它不适用于我 - http://joxi.net/LmG3OVNTRbEMam最后一行说'WebDriverException:消息:未知错误:无法处理扩展#1 来自未知错误:无法读取清单 (驱动程序信息:chromedriver = 2.29.461591(62ebf098771772160f391d75e589dc567915b233),platform = Windows NT 10.0.14393 x86_64)' – 2017-10-02 14:26:51

    1

    在获得硒github repo中的单元测试的启发后。这对我有效:

    from selenium import webdriver 
    
    PROXY_HOST = "IP_address" 
    PROXY_PORT = 8080 
    USERNAME = "your_user_name" 
    PASSWORD = "YOUR_PASSWORD" 
    
    profile = webdriver.FirefoxProfile() 
    profile.set_preference("network.proxy.type", 1) 
    profile.set_preference("network.proxy.http", PROXY_HOST) 
    profile.set_preference("network.proxy.http_port", PROXY_PORT) 
    profile.set_preference("network.proxy.socks_username", USERNAME) 
    profile.set_preference("network.proxy.socks_password", PASSWORD) 
    
    profile.update_preferences() 
    
    # executable_path = define the path if u don't already have in the PATH system variable. 
    browser = webdriver.Firefox(firefox_profile=profile) 
    browser.get('http://website.com') 
    browser.maximize_window()