Selenium using Python: enter/provide http proxy pa

2020-01-24 08:00发布

I want to use selenium with a proxy which is password protected. The proxy is not fixed, but a variable. So this has to be done in the code (just setting up firefox on this particular machine to work with the proxy is less-than-ideal). So far I have the following code:

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

At this point, the dialog pops up requesting the proxy user/pass.

Is there an easy way to either:

  1. Type in the user/pass in the dialog box.
  2. Provide the user/pass at an earlier stage.

4条回答
萌系小妹纸
2楼-- · 2020-01-24 08:19

Code worked for me

from selenium import webdriver

browser=webdriver.Firefox()

def login(browser):

    alert=browser.switch_to_alert()
    alert.send_keys("username"+webdriver.common.keys.Keys.TAB+"password")
    alert.accept() 
查看更多
唯我独甜
3楼-- · 2020-01-24 08:30

Did you try PROXY_HOST = "http://username:password@proxy.host.com"?

Also:

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

查看更多
冷血范
4楼-- · 2020-01-24 08:31

Selenium can't do that by itself. The only way I found helpful is described here. To be short, you need to add a browser extension on fly that does the authentication. It's much simpler than may seem to be.

Here is how it works for Chrome (in my case):

  1. Create a zip file proxy.zip containing two files:

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']
);

Don't forget to replace YOUR_PROXY_* to your settings.

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"
}
  1. Add the created proxy.zip as an extension

    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()
    

That's it. For me that worked like a charm. If you need to create proxy.zip dynamically or need PHP example then go to the original post

查看更多
家丑人穷心不美
5楼-- · 2020-01-24 08:33

After getting inspired by the unit tests in the selenium github repo. This worked for me:

from selenium import webdriver

PROXY_HOST = "IP_address"
PROXY_PORT = "your_proxy_port"
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()
查看更多
登录 后发表回答