Playwright Get Current URL Java

Profile picture for user arilio666

In this article, we will see how we can get the current URL in playwright using java. Using url(), we can fetch the current page URL it is in; this way, it will be helpful for testers to know precisely where they are operating.

 try (Playwright playwright = Playwright.create()) {
     Browser browser = playwright.chromium().launch(new   BrowserType.LaunchOptions().setHeadless(false));
            
            Page page = browser.newPage();
            page.navigate("http://www.programsbuzz.com/user/login");
            page.locator("#edit-name").type("Naruto");
            page.locator("#edit-pass").type("uzumaki");
            
            String currentUrl = page.url();
            System.out.println(currentUrl);
            
            browser.close();
            playwright.close();
}             
  • We can see here after inputting the username and password, we aksed the playwright to fetch the url of the current page and then close the test.
  • It has fetched the URL and printed it in the console as expected.