Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python+;SeleniumWebDriver:使用身份验证方法_Python_Selenium_Webdriver_Http Authentication - Fatal编程技术网

python+;SeleniumWebDriver:使用身份验证方法

python+;SeleniumWebDriver:使用身份验证方法,python,selenium,webdriver,http-authentication,Python,Selenium,Webdriver,Http Authentication,我正在使用python+SeleniumWebDriver自动化检查。 我被困在通过弹出窗口请求http身份验证的网站上 我试图通过以下代码使用“身份验证”方法: #init. driver = webdriver.Firefox() driver.get(url) #get to the auth popup window by clicking relevant link elem = driver.find_element_by_id("login_link") elem.click()

我正在使用python+SeleniumWebDriver自动化检查。 我被困在通过弹出窗口请求http身份验证的网站上

我试图通过以下代码使用“身份验证”方法:

#init.
driver = webdriver.Firefox()
driver.get(url)
#get to the auth popup window by clicking relevant link
elem = driver.find_element_by_id("login_link")
elem.click()
#use authenticate alert method
driver._switch_to.alert.authenticate("login", "password")
与此方法相关的(稀少的)infos/doc指示它应该提交提供的凭据并验证http auth。没有,我得到以下错误:

文件 “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/alert.py”, 第105行,第1行 self.driver.execute(Command.SET_ALERT_凭证,{'username':username,'password':password})文件 “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, 第201行,在execute中 self.error\u handler.check\u响应(响应)文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py”, 第159行,在check_响应中 引发异常\u类(值)selenium.common.exceptions.WebDriverException:消息:无法识别 指挥部:哨所 /会话/c30d03e1-3835-42f5-ace0-968aef486b36/警报/凭据

这里有没有我遗漏的东西/有没有人遇到同样的问题并解决了

PS:这个技巧在我的测试条件下不适用。

对于自动测试来说非常容易,不需要处理本机警报/对话框或其他浏览器差异

我在Java世界中非常成功地使用的方法是在代码中设置一个代理服务器,并注册一个
RequestInterceptor
,以拦截所有传入的请求(与所讨论的主机/URL模式相匹配)。当您有一个需要基本身份验证的请求时,
Authorization
HTTP头,其中包含所需的凭据('Basic'+Base64编码的'user:pass'字符串。因此对于'foo:bar',您需要设置值
Basic Zm9vOmJhcg==

启动服务器,将其设置为web代理,当发出需要身份验证的请求时,代理将添加标题,浏览器将看到标题,验证凭据,而无需弹出对话框

尽管这项技术看起来很费劲,但通过为每个请求自动设置标题,您不必显式地将
user:pass@
添加到任何可能需要它的URL,因为在授权区域中有多种方式。此外,与
user:pass@
用户不同,您不必担心浏览器缓存(或在一定时间后停止缓存)标头,也不必担心跨HTTP/HTTPS

这种技术工作得很好,但是如何在Python中实现这一点呢

您可以使用它,它在Python中公开了它的RESTAPI。这是您需要的剩余通话:

POST/proxy/[port]/headers-设置并覆盖HTTP请求头。 例如,设置自定义用户代理。有效负载数据应该是json 编码的标题集(非url编码)

因此,根据前面的示例(伪代码):

POST localhost:8787/proxy//headers'{“Authorization”:“Basic Zm9vOmJhcg==”}

或者,您也可以使用Twisted为自定义Python代理服务器进行验证。

在URL中可以进行基本身份验证,但您必须设置首选项:

from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("network.http.phishy-userpass-length", 255)
driver = webdriver.Firefox(profile)
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth")
如果它在您的情况下不起作用,那么它就不是基本的身份验证

from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("network.http.phishy-userpass-length", 255)
driver = webdriver.Firefox(profile)
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth")