如何使用python selenium设置代理认证(用户和密码) [英] How to set proxy authentication (user & password) using python selenium

查看:2776
本文介绍了如何使用python selenium设置代理认证(用户和密码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 2.7中使用Firefox webdriver和Selenium。我的python程序启动Firefox浏览器,并在运行程序时访问不同的网站。但是,我需要设置代理身份验证,以便程序访问任何网站时,它将通过代理服务器访问。

有一些类似的SO上的quetions。但是,Python的Selenium Firefox webdriver没有具体的解决方案。


解决方案

附带说明,使用已保存凭证的配置文件运行Firefox。你可以加载一个在 loginTextbox password1Textbox chrome:/ /global/content/commonDialog.xul (altert窗口)。



已经有一些扩展可以完成这项工作。例如:关闭代理身份验证



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)
凭证= 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

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)

这篇关于如何使用python selenium设置代理认证(用户和密码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆