python + selenium webdriver:使用身份验证方法 [英] python + selenium webdriver : using authenticate method

查看:57
本文介绍了python + selenium webdriver:使用身份验证方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using python + selenium webdriver to automatize checks. I am stuck on websites that request http authentication through popup window.

我正在尝试通过以下代码使用身份验证"方法:

I am trying to use the "authenticate" method through the following code :

#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")

与此方法相关的(稀缺)信息/文档表明它应该提交提供的凭据&验证 http 身份验证.它没有,我收到以下错误:

the (scarce) infos/doc related to this method indicates that it should submit the credentials provided & validate http auth. It doesn't and I am getting the following error :

文件"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/alert.py",第 105 行,在认证中self.driver.execute(Command.SET_ALERT_CREDENTIALS, {'username':username, 'password':password}) 文件"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",第 201 行,执行中self.error_handler.check_response(response) 文件/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py",第 159 行,在 check_response 中引发 exception_class(value) selenium.common.exceptions.WebDriverException:消息:无法识别指挥所/session/c30d03e1-3835-42f5-ace0-968aef486b36/alert/credentials

File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/alert.py", line 105, in authenticate self.driver.execute(Command.SET_ALERT_CREDENTIALS, {'username':username, 'password':password}) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 159, in check_response raise exception_class(value) selenium.common.exceptions.WebDriverException: Message: Unrecognized command: POST /session/c30d03e1-3835-42f5-ace0-968aef486b36/alert/credentials

我在这里遗漏了什么/有人遇到过同样的问题并解决了吗?

is there something I am missing here / has anybody come accross the same issue and resolved it ?

PS:http://username:password@url 技巧在我的测试中对我不起作用条件.

PS : the http://username:password@url trick doesn't work for me in my tests conditions.

推荐答案

基本认证 很容易解决自动化测试问题,无需处理本机警报/对话框或其他浏览器差异.

Basic authentication is pretty easy to work around for automated testing, without having to deal with native alerts/dialogs or other browser differences.

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

The approach I've used very successfully in the Java world is to set up a Browsermob proxy server in code and register a RequestInterceptor to intercept all incoming requests (that match the host / URL pattern in question). When you have a request that would otherwise need Basic auth, add an Authorization HTTP header with the credentials required ('Basic ' + the Base64-encoded 'user:pass' string. So for 'foo:bar' you'd set the value Basic Zm9vOmJhcg==)

启动服务器,将其设置为 Web 代理 Selenium 流量,当发出需要认证的请求时,代理会添加header,浏览器会看到,验证凭据,不需要弹出对话框.

Start the server, set it as a web proxy for Selenium traffic, and when a request is made that requires authentication, the proxy will add the header, the browser will see it, verify the credentials, and not need to pop up the dialog.

虽然该技术看起来很费力,但通过为每个请求自动设置标头,您不必将 user:pass@ 显式添加到 任何 URL可能需要它,在那里有多种方式进入授权区域.此外,与 user:pass@ 用户不同,您不必担心浏览器缓存(或在一定时间后停止缓存)标头,或跨越 HTTP/HTTPS.

Although the technique might seem laborious, by having the header set automatically for every request, you don't have to explicitly add user:pass@ to any URL that might need it, where there are multiple ways into the auth-ed area. Also, unlike user:pass@ users, you don't have to worry about the browser caching (or ceasing to cache, after a certain amount of time) the header, or about crossing HTTP/HTTPS.

这种技术效果很好,但如何在 Python 中实现这一点?

That technique works very well, but how to achieve this in Python?

您可以使用这个 Browsermob 的 Python 包装器,它在Python.这是您需要的 REST 调用:

You could use this Python wrapper for Browsermob, which exposes its REST API in Python. This is the REST call you'll need:

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

POST /proxy/[port]/headers - Set and override HTTP Request headers. For example setting a custom User-Agent. Payload data should be json encoded set of headers (not url-encoded)

所以,从前面的例子(伪代码):

So, from the earlier example (pseudocode):

POST localhost:8787/proxy/<proxy_port>/headers '{"Authorization": "Basic Zm9vOmJhcg=="}'

或者,您可以查看此答案,了解使用 Twisted 的自定义 Python 代理服务器.

Alternatively, you could see this answer for a custom Python proxy server using Twisted.

这篇关于python + selenium webdriver:使用身份验证方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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