Search code examples
pythonplaywrightplaywright-python

How to authenticate using Windows Authentication in Playwright


I need to automate a test that uses Windows Authentication.

I know that the the prompt that opens up is not part of the HTML page, yet why my code is not working:

login_page.click_iwa()
sleep(5)
self.page.keyboard.type('UserName')
sleep(5)
self.page.keyboard.press('Tab')
self.page.keyboard.type('Password')

Solution

  • I solved the issue by using a virtual keyboard:

    from pyautogui import press, typewrite, hotkey
    
    
    def press_key(key):
        press(key)
    
    
    def type_text(text):
        typewrite(text)
    
    
    def special_keys(special, normal):
        hotkey(special, normal)
    

    Then, after I implemented the virtual keyboard, I did this:

    def login_iwa(self, user_name='User321', password='123456', need_to_fail=False):
            pyautogui.FAILSAFE = False
            self.click_iwa()
            sleep(7)
            type_text(user_name)
            sleep(1)
            press_key('Tab')
            sleep(1)
            type_text(password)
            sleep(1)
            press_key('Enter')
            sleep(2)
            if need_to_fail:
                press_key('Tab')
                sleep(1)
                press_key('Tab')
                sleep(1)
                press_key('Tab')
                sleep(1)
                press_key('Enter')
                sleep(1)
    

    I used pyautogui.FAILSAFE = False because sometime the popup was hiding behind the screen or was openning up on the 2nd screen.