How to set proxy authentication (user & password) using Python + Selenium

后端 未结 4 411
栀梦
栀梦 2020-11-30 07:52

I am using Firefox WebDriver in Python 2.7 with Selenium. My python program starts Firefox browser and visits different websites when I run the program. But, I need to set t

4条回答
  •  没有蜡笔的小新
    2020-11-30 08:28

    There is an example for Firefox + Python but without the authentication here. Then you can find other available parameters here in source code. So it looks like you need the following:

    socksUsername
    socksPassword
    

    For example:

    from selenium import webdriver
    from selenium.webdriver.common.proxy import *
    
    myProxy = "host:8080"
    
    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy, # set this value as desired
        'ftpProxy': myProxy,  # set this value as desired
        'sslProxy': myProxy,  # set this value as desired
        'noProxy': ''         # set this value as desired
        'socksUsername': = ''
        'socksPassword': = ''
        })
    
    driver = webdriver.Firefox(proxy=proxy)
    

    or with preferences:

    driverPref = webdriver.FirefoxProfile()
    driverPref.set_preference("network.proxy.type", 1)
    .
    .
    .
    driverPref.set_preference('network.proxy.socks', proxyHost)
    driverPref.set_preference('network.proxy.socks_port', proxyPort)
    driverPref.update_preferences()
    
    driver = webdriver.Firefox(firefox_profile=driverPref)
    

    EDIT:

    I looked at it again and it seems that it is impossible to set authentication details in FF, even manually. The only way is just to remember the details that you have already entered which done by 2 parameters:

    signon.autologin.proxy=true
    network.websocket.enabled=false
    

    that can be configured with the set_preference() method. You can also manually view all FF options by browsing to about:config.

提交回复
热议问题