6

After opening application URL, User is redirected to Sign-In page where there is a Sign-In button.

driver.get("abc.com")

Now when user click on Sign-In button, URL is changed in the same window say it becomes xyz.com and shows authentication popup window for login purpose similar to image shown below. enter image description here

To enter username and password in authentication window I tried the below code

shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("username")
time.sleep(1)
shell.Sendkeys("{TAB}")
time.sleep(1)
shell.Sendkeys("password") 
time.sleep(1)
shell.Sendkeys("{ENTER}")
time.sleep(1)

It didn't work. Then I tried to open windows authentication popup directly(by copying URL after click of Sign-In button) with the above code, it worked

driver.get("xyz.com")//instead of abc.com my application URL

I am bit confused. If I open my app URL abc.com, click on Sign-In button, use autoit, it didn't enter credentials. But if I directly send window authentication URL xyz.com instead of app URL abc.com and use autoit, it work.

Can anyone suggest what I am missing here? I also tried to switch window after click on Sign-In button thinking its new URL and then autoit command, but it still it didn't work.

driver.switch_to_window(driver.window_handles[1])

Any idea on this?

Note: I noticed that click on Sign-In button, windows is loading infinitely & cursor is active on username text-field of windows authentication poup. Also once windows authentication window appears, none of selenium command is working and neither autoit command.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Shoaib Akhtar
  • 1,393
  • 5
  • 17
  • 46
  • Possible duplicate of [Python Windows Authentication username and password is not working](https://stackoverflow.com/questions/45328654/python-windows-authentication-username-and-password-is-not-working/45329228#45329228) – undetected Selenium Feb 16 '18 at 09:33
  • @DebanjanB My scenario is different. When I opened the application says abc.com, it does not show authentication popup. Now when user clicks on a sign-in button then windows authentication popup appears and url is changed say xyz.com. This is the reason passing username and password with first url is not working. Any idea how to resolve it? – Shoaib Akhtar Feb 19 '18 at 08:00
  • Try to open the `url` which appears post clicking on _sign-in_ button along with the credentials. – undetected Selenium Feb 19 '18 at 08:03
  • You meant to say I should use driver.get() function twice? First with application and then 2nd after click on Sign-In button? – Shoaib Akhtar Feb 19 '18 at 08:16

6 Answers6

4

The dialog is treated as an alert by selenium. In c#, the code to enter in credentials for a Firefox test looks like:

// Inputs id with permissions into the Windows Authentication box
var alert = driver.SwitchTo().Alert();
alert.SendKeys( @"username" + Keys.Tab + 
                @"password" + Keys.Tab);
alert.Accept();

The first line is telling the driver to check for open alerts (the dialog box).

The second line sends the username and password to the alert

The third line then sends those credentials and, assuming they are valid, the test will continue.

Assuming you are testing using Firefox, there is no requirement to use extra frameworks to deal with this authentication box.

Rescis
  • 547
  • 5
  • 19
2

you can automate the keyboard:

import keyboard
keyboard.write("username")
keyboard.press_and_release("tab")
keyboard.write("password")
keyboard.press_and_release("enter")

Here is an example of loading a login page with selenium, then entering login credentials with keyboard:

from selenium.webdriver import Firefox
import keyboard

driver = Firefox()
driver.get('https://somelink')

keyboard.press_and_release('tab')
keyboard.press_and_release('shift + tab')
keyboard.write('user', delay=1)
keyboard.press_and_release('tab')
keyboard.write('pass', delay=1)
keyboard.press_and_release('enter')

Note: keyboard may require root permission on Linux.

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
Sahil Agarwal
  • 555
  • 3
  • 12
1

It looks like you just have to open the URL with basic auth credentials. Can you try this first?

driver.get('http://username:password@abc.com')

If you're still getting the popup, try this

driver.get('http://username:password@xyz.com') #assuming this is the site that handles authentication
driver.get('abc.com')

It should stop the popup

Aldo Suwandi
  • 382
  • 1
  • 6
  • 20
0

You need to switch to the alert, which is different than a window. Once the alert is present, you switch the alert handle, then use the .authenticate method to give it the username and password

alert = driver.switch_to.alert
alert.authenticate(username, password)

You may want to wait on the EC.alert_is_present expected condition to be sure the alert is present.

sytech
  • 29,298
  • 3
  • 45
  • 86
-1

Try this (with page_title being the title of the popup window and assuming you are on a windows machine) :

from win32com.client import Dispatch
autoit = Dispatch("AutoItX3.Control")

def _window_movement_windows(page_title):
        autoit.WinSetOnTop(page_title, "", 1)
        autoit.WinActivate(page_title, "")
        autoit.WinWaitActive(page_title)

An example how to setup AutoIt with python can be found here : Calling AutoIt Functions in Python

Chuk Ultima
  • 987
  • 1
  • 11
  • 21
-1

With Firefox, it's possible to avoid the authentication popup by automatically providing the credentials when they are requested.

It requires to inject and run some code at a browser level to detect the authentication attempt and set the credentials.

Here's a working example:

from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver


def add_credentials_moz(driver, target, username, password):
    driver.execute("SET_CONTEXT", {"context": "chrome"})        
    driver.execute_script("""
        let [target, username, password] = [...arguments];
        let WebRequest = Cu.import('resource://gre/modules/WebRequest.jsm', {});
        WebRequest.onAuthRequired.addListener(function listener(){
          WebRequest.onAuthRequired.removeListener(listener);
          return Promise.resolve({authCredentials: {username: username, password: password}});
        }, {urls: new MatchPatternSet([target])}, ['blocking']);
        """, target, username, password)        
    driver.execute("SET_CONTEXT", {"context": "content"})

WebDriver.add_credentials_moz = add_credentials_moz



driver = webdriver.Firefox()
driver.add_credentials_moz("https://httpbin.org/*", username="user", password="passwd")

driver.get("https://httpbin.org/")

driver.find_element_by_css_selector("[href='/basic-auth/user/passwd']")\
      .click()
Florent B.
  • 41,537
  • 7
  • 86
  • 101