Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle windows authentication popup in selenium using python(plus java)

enter image description here

I tried the below code, but it didn't work for me

from selenium import webdriver
driver=webdriver.Chrome('D:/BrowsersDriver/chromedriver.exe')
driver.get('https://username:[email protected]/')

Later on I tried to use the same approach in Java

driver.get('https://username:[email protected]/')

But unfortunately it didn't work for me in any browser. Am I missing something here?

Then I tried with AutoIT in Java

Runtime.getRuntime().exec("D:\\FirefoxWindowAuthentication.exe");
driver.get("https://www.engprod-charter.net/")

It works well in Firefox & IE, but didn't work for Chrome. Is there any way that at-least I can achieve this in selenium using python & what I am missing in case of Java. Please suggest me any solution, tried a lot

like image 499
Shoaib Akhtar Avatar asked Nov 18 '16 07:11

Shoaib Akhtar


2 Answers

I got this solution which is working well for all three browser(Firefox, Chrome and IE).

from selenium import webdriver
import time
import win32com.client

driver=webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.engprod-charter.net/')
shell = win32com.client.Dispatch("WScript.Shell")   
shell.Sendkeys("username")  
time.sleep(2)
shell.Sendkeys("{TAB}")
time.sleep(2)
shell.Sendkeys("password") 
time.sleep(2)
shell.Sendkeys("{ENTER}")
time.sleep(2)
driver.quit()

Note : Install win32com.client if you have not downloaded. To install win32com.client use below command

pip install pypiwin32
like image 132
Shoaib Akhtar Avatar answered Oct 20 '22 14:10

Shoaib Akhtar


Try following solution and let me know in case of any issues:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys 
import time

driver=webdriver.Firefox()
driver.get('https://www.engprod-charter.net/')
time.sleep(3)
driver.switch_to.alert.send_keys(username + Keys.TAB + password)
time.sleep(3)
driver.switch_to.alert.accept()

Here is the solution for IE on Windows using third-party autoHK lib

like image 36
Andersson Avatar answered Oct 20 '22 13:10

Andersson