使用WebDriver获取过去的IE身份验证

时间:2014-04-17 20:59:46

标签: internet-explorer python-2.7 authentication selenium-webdriver

我想知道是否有人使用Webdriver和Python来导航IE中弹出的用户身份验证窗口。

我已经建议使用AutoIT,但我想保持我的解决方案严格的Python。

我尝试了python-ntlm,但在运行下面的脚本时仍然遇到“需要授权”错误:

import urllib2
from ntlm import HTTPNtlmAuthHandler

user = r'userName'
password = 'password'

url = 'url goes here'

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)

# Create the NTLM Authentication Handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

# Create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

# retrieve the result
response = urllib2.urlopen(url)
print response.read()

我想知道其他人是如何处理的? 下面是我尝试使用window_handles调用的webdriver脚本:

    def test_ie_navigation(self):
    user = 'userName'
    password = 'passWord'

    driver = self.driver
    driver.get("url I am going to")
    aw = driver.window_handles
    auth_window = driver.switch_to.window(aw[1])
    auth_window.sendKeys(user)
    auth_window.sendKeys(Keys.TAB)
    auth_window.sendKeys(password)
    auth_window.snedKeys(Keys.ENTER)

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并使用PyAutoIt解决了它,所以我的代码仍然是纯python。

import autoit
autoit.win_wait_active("Windows Security")
autoit.send(user['username'])
autoit.send("{TAB}")
autoit.send(user['password'])
autoit.send("{ENTER}")

以上是我的代码,适用于我当前的设置(Python 3,Internet Explorer 11,Windows 8.1机器)。

答案 1 :(得分:0)

请尝试以下代码:

def test_ie_navigation(self):
user = 'userName'
password = 'passWord'

driver = self.driver
driver.get("http://<USERNAME>:<PASSWORD>@<url I am going to (Note: Don't give http/https here as it is already appended in the beginning)>") # e.g. driver.get("http://<USERNAME>:<PASSWORD>@google.co.in")
aw = driver.window_handles
auth_window = driver.switch_to.window(aw[1])
auth_window.sendKeys(user)
auth_window.sendKeys(Keys.TAB)
auth_window.sendKeys(password)
auth_window.sendKeys(Keys.ENTER)

看看这是否有帮助!