1

My below code for windows authentication was working well for over a year. But for the last few days it is longer working now. I am not able to identify exact reason for that.

from selenium import webdriver
import time
import win32com.client

driver=webdriver.Chrome('D:/Software/BrowsersDriver/ChromeDriver/chromedriver.exe')
driver.maximize_window()
driver.get("authentication windows url")
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(5)
driver.quit()

I tried to change my chrome driver to latest when it stops working, but still it is not working. Once the windows authentication appears, cursor keeps blinking on username text-field infinitely, but username is not entered. I am getting below message in console when it is stuck

2018-04-02 07:09:28,230 INFO: Imported existing <module 'comtypes.gen' from 'C:\Python27\lib\site-packages\comtypes\gen\__init__.pyc'>
2018-04-02 07:09:28,230 INFO: Using writeable comtypes cache directory: 'C:\Python27\lib\site-packages\comtypes\gen'

The same code used to work well earlier. I am using python 2.7.12 and latest version of chrome driver.

Also if someone has a different solution or a link for it for providing credentials in authentication window in python selenium then that too will work for me.

Note: The same code works well earlier, but now it's not working

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 Mar 26 '18 at 12:21
  • No, its not duplicate. The solution of passing credential in URL does not work for me... I am using python 2.7.12 and latest chrome and latest chrome driver – Shoaib Akhtar Mar 26 '18 at 12:48
  • It's hard to say why it's no longer working without a reproducible example. Why not provide the credentials in the URL or with an injected XMLHttpRequest ? – Florent B. Apr 02 '18 at 14:18
  • @FlorentB. Regarding XMLHttpRequest do you have any example or link to it. And regarding providing credential in url also does not work https://stackoverflow.com/questions/40671662/how-to-handle-windows-authentication-popup-in-selenium-using-pythonplus-java – Shoaib Akhtar Apr 02 '18 at 14:29
  • @Shoaib Akhtar, providing the credentials in the URL is possible with Chrome when the URL is composed of the domain only without any path. Your link points to an issue with Firefox, not Chrome. – Florent B. Apr 02 '18 at 14:42
  • I tried passing credential in url in this format driver.get('https://username:password@www.engprod-charter.net/').. It does not work for me either in Chrome or Firefox. No idea whether I am missing something here – Shoaib Akhtar Apr 02 '18 at 15:02
  • There's a redirection, thus a different domain. Try with the landing domain `https://username:password@www.engprod-spectrum.net` – Florent B. Apr 02 '18 at 16:13
  • @FlorentB. The new url is also not woking.. driver.get("https://username:password@www.engprod-spectrum.net").... I tried similarly changing url for different page of my application, that too not working – Shoaib Akhtar Apr 03 '18 at 10:12

3 Answers3

1

Did you try

driver.switchTo().alert();

or

driver.switchTo().activeElement();

before typing credentials?

However, if this works I can't explain why it worked before.

AcMuD
  • 131
  • 9
  • yes tried it, it does not work. If I debug code control does not go to next statement when windows authentication popup appears, switch to alert would have if there was browser based popup but in my case it is window based popup – Shoaib Akhtar Apr 06 '18 at 09:42
  • Ok...a completely other solution could be to use Java.awt.Robot class? It's not addicted to a browser. – AcMuD Apr 06 '18 at 10:49
  • Means, you write a method with a two strings as parameter (one for user, one for Password) and let the robot press and release every single key using a loop. – AcMuD Apr 06 '18 at 10:56
  • since I am working with python and selenium. I can't use Robot class as it is from Java – Shoaib Akhtar Apr 06 '18 at 11:19
  • Sorry, my fault. But maybe this can help you: https://stackoverflow.com/questions/860013/is-there-a-python-equivalent-to-javas-awt-robot-class – AcMuD Apr 06 '18 at 11:25
1

Try AutoIT to create an exe for the windows authentication and call it where ever required, if nothing else is working. If you are mostly working with Pyton then try this

Sample code:-

WinWaitActive("Authentication Required","","120")

If WinExists("Authentication Required") Then
Send("username{TAB}")

Send("password{Enter}")

EndIf
SIGSTACKFAULT
  • 919
  • 1
  • 12
  • 31
sunny_teo
  • 1,961
  • 1
  • 7
  • 11
0

You could try automating the keyboard directly?? It might work. You automate the keyboard.

Answered a similar question here - https://stackoverflow.com/a/49109132/6003362

Sahil Agarwal
  • 555
  • 3
  • 12
  • I tried your code as provided in the link. When I tested with url like google("https://www.google.co.in/"), it does type username... but when I provide url where authentication is required("https://www.engprod-charter.net/"), it does not type username and neither anything on console, does nothing after opening the url, cursor keeps blinking on username text-field and program hangs for a while and then timeout exception – Shoaib Akhtar Apr 11 '18 at 05:10