How to get the title of a Page Using Playwright Java?

The page title carries important information that can be used for verification, validation, or further processing in your automated scripts. This article explores how to extract the page title using Playwright Java.

There are a few methods available to retrieve the title of a page, as outlined below.

Let’s discuss them one by one.

title() method

title() method of the Page interface is one of the most commonly used methods to retrieve the title of a Page.

For our example, let’s refer to this page ( https://testkru.com/Elements/TextFields ) titled “Text Fields“. We’ll attempt to retrieve it by utilizing the title() method.

Navigate to the Page
		Playwright playwright = Playwright.create();
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating a page
		Page page = browserContext.newPage();

		// Navigating to the URL
		page.navigate("https://testkru.com/Elements/TextFields");

Retrieve the title
page.title();

Here is the complete code for using the title() method to retrieve the title.

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

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating a page
		Page page = browserContext.newPage();

		// Navigating to the URL
		page.navigate("https://testkru.com/Elements/TextFields");

		System.out.println("Title of the page: " + page.title());

		// closing the instances
		browser.close();
		playwright.close();

	}

}

Output –

Title of the page: Text Fields

It appears that the title was retrieved successfully. Another way to achieve this is through the evaluate() method, which we will now discuss.

evaluate() method

The evaluate() method in Playwright allows for the execution of JavaScript code. So, we only have to know the javascript code to get the Page’s title.

To retrieve the title in Javascript, the “document.title” can be used. This can be executed through the evaluate() function in Playwright.

page.evaluate("document.title")

Here is the whole code utilizing the evaluate() method to retrieve the Page’s title.

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

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating a page
		Page page = browserContext.newPage();

		// Navigating to the URL
		page.navigate("https://testkru.com/Elements/TextFields");

		System.out.println("Title of the page: " + page.evaluate("document.title"));

		// closing the instances
		browser.close();
		playwright.close();

	}

}

Output –

Title of the page: Text Fields

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *