Rayrun
← Back to Discord Forum

Handling authentication window

Hi, I am new to playwright. When I enter my company internal website address there is a new window(Microsoft authentication page) that automatically pops up and once I enter the username and pwd the pop up/login window automatically closes and the content on my company website starts to load. I am trying to repeat this in playwright and when I navigate to the page using page.goto, I can see a new authentication window opening up. But, I am not able to switch to that authentication window to fill in my username and pwd. Can anyone help me how to handle switching to authentication page and switching back to actual website once authentication is done? I tried using page.on("page", handle_page) from the playwright docs but I never see anything getting printed out. Any help is appreciated since I am stuck on this for a while. Following is my piece of code.

from playwright.sync_api import Page, expect, sync_playwright,Playwright import os

def incercept_request(request): print('requested URL: ', request.url)

def incercept_response(response): print(f'response URL:{response.url}, Status:{response.status}')

def handle_page(page): page.wait_for_load_state() print('new page title:{}'.format(page.title()))

def run(playwright: Playwright) -> None: app_data_path = os.getenv("LOCALAPPDATA") user_data_path = os.path.join(app_data_path,'Chromium\UserData\Default') browser = playwright.chromium.launch_persistent_context(user_data_path,channel='chrome',headless=False,timeout=0)

page1 = browser.new_page() 
page1.goto("Internal Website Link Here")   
page1.on("page", handle_page)

with sync_playwright() as playwright: run(playwright)

This thread is trying to answer question "How to handle switching to an authentication page and switching back to the main website after authentication in Playwright?"

4 replies
bububu8615
bububu8615

you should try page1.on("popup") or "alert" I don't think page works in this case

@bububu8615: I tried using popup as well. Still the same. Any other recommendations?

I tried using popup as well. Still the same. Any other recommendations?

@lucgagan: Thanks. Any reference material for python? This link is in Javascript.

Thanks. Any reference material for python? This link is in Javascript.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.