如何将HttpAuthentication传递给Selenium WebDriver [英] How to pass over the HttpAuthentication into selenium webdriver

查看:478
本文介绍了如何将HttpAuthentication传递给Selenium WebDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的python项目中,登录过程由HttpBasicAuth处理程序处理.得到响应后,我应该通过硒加载主页.

In my python project, the login process is handled by the HttpBasicAuth handler. After getting the response I should load the main page via selenium.

应该怎么做?

它具有baseurl.当基本网址由python-selenium加载时,弹出框会要求输入用于验证的用户名和密码.然后进入主页.

It has the baseurl. When the base url is loaded by python-selenium, the pop-up box asks for the username and password for authentication. Then it enters into main page.

这是两个步骤:

  • 第1步:登录(使用HttpBasicAuth处理程序)

我使用HttpBasicAuth处理程序的原因是,当我打开url时,它会打开一个用于登录的弹出窗口.由于我们不知道Selenium会处理弹出模式对话框窗口的控件,因此我们切换到HttpAuthentication.

The reason I am using the HttpBasicAuth handler is that when I open my url it opens a popup window for login. Since, we didn't know the control of the popup-modal dialog window to be handled by Selenium, we switched to HttpAuthentication.

(这是我需要建议的部分.)

  • 第2步:获得响应后,处理硒中的其余步骤.

在此中间部分中,我使用HttpBasicAuth处理程序的响应进行身份验证,以便Selenium可以继续进行测试中的其他步骤.

In this intermediate part I use the response from HttpBasicAuth handler to authenticate so that Selenium can continue with the other steps in the test.

如何使网站在Selenium中进行身份验证?我是否必须使用HttpBasicAuthentication Response为Selenium Webdriver创建cookie,以跳过登录框?

How can I make the site authenticated in Selenium? Do I have to create a cookie for Selenium webdriver by using HttpBasicAuthentication Response so as to skip the login box?

推荐答案

通过在HTTP响应中添加标头可以轻松解决此问题,很遗憾,Selenium2不支持此功能.

This is a problem that could easily be solved by adding header to your HTTP response, unfortunately Selenium2 does not support this feature.

您可以通过在URL中嵌入用户名和密码来尝试这样使用它:

You could try using it like this by embeddeing the username and password in the URL:

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://myusername:mypassword@www.yourpage.com") # Load page

请注意,由于安全问题,这不适用于IE,但是您可以通过稍微修改注册表来使其工作.

Note this does not work for IE (due to security issues), but you can make it work by modifying the registry a bit.

根据您的评论,我不太了解您的问题.如果您按照我的指定执行第一个请求,浏览器将自动存储一个特殊的Authorization:标头,其中密码和用户名已连接并以base64编码.现在将对所有其他请求进行身份验证,直到您关闭浏览器.

As per your comment, I do not really understand your question. If you do the first request as I specified, the browser will automatically store a special Authorization: header with the password and username concatenated and base64-encoded. All the other requests will be now authenticated until you close the browser.

此方法与Selenium 1或Selenium 2无关,但特定浏览器支持什么. IE是我所知道的唯一限制此功能的浏览器.

This method has little to do with Selenium 1 or Selenium 2, but what the specific browser supports. IE is the only browser that I know that restricts this.

这篇关于如何将HttpAuthentication传递给Selenium WebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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