Thursday, 1 June 2023

Playwright: Locate the element by text content

Using page.getByText() method, we can locate the elements by text content.

 

Signature

Locator getByText(String text)
Locator getByText(String text, GetByTextOptions options)
Locator getByText(Pattern text)
Locator getByText(Pattern text, GetByTextOptions options)

 

Example

page.getByText("Hello there!!!").allTextContents();

Pattern helloPattern = Pattern.compile("hello", Pattern.CASE_INSENSITIVE);
page.getByText(helloPattern).allTextContents();

page.getByText("hello", new Page.GetByTextOptions().setExact(true)).allTextContents()

Find the below working application.

 

locatorByTextContent.html

<!DOCTYPE html>
<html>

<body>

	<h1>Page text content demo</h1>

	<p>hello world!!!!</p>
	<p>Hello there!!!</p>
	<p>>Hey buddy</p>
	<p>say hello</p>
	<p>hello</p>
	<p>HELlo</p>
	
</body>

</html>

FileUtil.java

package com.sample.app.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class FileUtil {

	public static String resourceAsString(String resourceName) throws IOException {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceName);
		if (url == null) {
			return null;
		}

		URLConnection urlConnection = url.openConnection();

		urlConnection.setUseCaches(false);

		try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
			char[] buffer = new char[1048];
			StringBuilder builder = new StringBuilder();

			int count = -1;
			while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
				builder.append(buffer, 0, count);
			}

			return builder.toString();
		}

	}
}

GetLocatorByTextContent.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class GetLocatorByTextContent {
	public static void main(String[] args) throws IOException {
		try (Playwright playwright = Playwright.create()) {

			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));

			final String content = FileUtil.resourceAsString("locators" + File.separator + "locatorByTextContent.html");

			Page page = browser.newPage();
			page.setContent(content);

			System.out.println("All the locators match to the text 'Hello there!!!'");
			List<String> textContent = page.getByText("Hello there!!!").allTextContents();
			for (String str : textContent) {
				System.out.println(str);
			}

			System.out.println("\nAll the locators contains the text 'hello' and case insensitive");
			Pattern helloPattern = Pattern.compile("hello", Pattern.CASE_INSENSITIVE);
			textContent = page.getByText(helloPattern).allTextContents();
			for (String str : textContent) {
				System.out.println(str);
			}

			System.out.println("\nAll the locators contains the text 'hello' only");
			textContent = page.getByText("hello", new Page.GetByTextOptions().setExact(true)).allTextContents();
			for (String str : textContent) {
				System.out.println(str);
			}

		}
	}
}

Output

All the locators match to the text 'Hello there!!!'
Hello there!!!

All the locators contains the text 'hello' case insensitive
hello world!!!!
Hello there!!!
say hello
hello
HELlo

All the locators contains the text 'hello' case insensitive
hello




Previous                                                 Next                                                 Home

No comments:

Post a Comment