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

后端 未结 4 409
栀梦
栀梦 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

    In addition to running Firefox with a profile which has the credentials saved. You can do it loading an extension that writes in the loginTextbox and password1Textbox of chrome://global/content/commonDialog.xul (the alert window).

    There are already some extensions that will do the job. For instance: Close Proxy Authentication

    https://addons.mozilla.org/firefox/downloads/latest/close-proxy-authentication/addon-427702-latest.xpi

    from selenium import webdriver
    from base64 import b64encode
    
    proxy = {'host': HOST, 'port': PORT, 'usr': USER, 'pwd': PASSWD}
    
    fp = webdriver.FirefoxProfile()
    
    fp.add_extension('closeproxy.xpi')
    fp.set_preference('network.proxy.type', 1)
    fp.set_preference('network.proxy.http', proxy['host'])
    fp.set_preference('network.proxy.http_port', int(proxy['port']))
    # ... ssl, socks, ftp ...
    fp.set_preference('network.proxy.no_proxies_on', 'localhost, 127.0.0.1')
    
    credentials = '{usr}:{pwd}'.format(**proxy)
    credentials = b64encode(credentials.encode('ascii')).decode('utf-8')
    fp.set_preference('extensions.closeproxyauth.authtoken', credentials)
    
    driver = webdriver.Firefox(fp)
    

提交回复
热议问题