DarthOpto
DarthOpto

Reputation: 1672

Getting Past IE Authentication with WebDriver

I was wondering if anyone has used Webdriver with Python to navigate the User Authentication window which pops up in IE.

I have had it suggested to use AutoIT, but I would like to keep my solution strictly Python.

I have tried python-ntlm but keep getting stuck on an Authorization Required error when I run the script below:

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()

I am wondering how others have handled this? Below is my webdriver script where I have tried using the window_handles call:

    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)

Upvotes: 1

Views: 2010

Answers (2)

ldiary
ldiary

Reputation: 494

I encountered the same problem and solved it using PyAutoIt, so my code remains pure python.

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

Above is my code that works on my current setup (Python 3, Internet Explorer 11, Windows 8.1 machine).

Upvotes: 1

Sitam Jana
Sitam Jana

Reputation: 3129

Try the following code:

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)

See if this helps!

Upvotes: 0

Related Questions