如何使用python selenium设置代理认证(用户和密码) [英] How to set proxy authentication (user & password) using python selenium
问题描述
有一些类似的SO上的quetions。但是,Python的Selenium Firefox webdriver没有具体的解决方案。
附带说明,使用已保存凭证的配置文件运行Firefox。你可以加载一个在 loginTextbox
和 password1Textbox
已经有一些扩展可以完成这项工作。例如:关闭代理身份验证
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)
凭证= b64encode(credentials.e解码('utf-8')
fp.set_preference('extensions.closeproxyauth.authtoken',凭证)
驱动程序= webdriver.Firefox(fp)
I am using Firefox webdriver in Python 2.7 with Selenium. My python program start firefox browser and visit different websites when I run the program. But, I need to set the proxy with authentication, so that when program visit any website, it will visit through the proxy server.
There are some similar quetions on SO. But, there is no specific solution for Selenium Firefox webdriver of Python.
In adittion 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 altert window).
There are already some extensions that will do the job. For instance: Close Proxy Authentication
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)
这篇关于如何使用python selenium设置代理认证(用户和密码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!