Tuesday, 20 June 2023

Playwright: How to get the current page url?

Using ‘page.url()’ method, we can get the current page url.

 

GetCurrentPageURL.java

package com.sample.app.miscellaneous;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class GetCurrentPageURL {
	
	public static void main(String[] args) {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium().launch();
			Page page = browser.newPage();
			page.navigate("https://self-learning-java-tutorial.blogspot.com/");
			System.out.println(page.url());
			
		}
	}

}

 

Output

https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html

  Since the url "https://self-learning-java-tutorial.blogspot.com/” is redirected to “https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html”, you can see this in the output.

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment