Thursday, 13 July 2023

Playwright: Get elements relative to an ElementHandle

Using ElementHandle, we can get the parent, grandparent, siblings, childs etc.,

 

Example

ElementHandle elementHandle = page.querySelector("#para1");
ElementHandle grandParent = elementHandle.querySelector("xpath=../..");
List<ElementHandle> siblings = elementHandle.querySelectorAll("xpath=following-sibling::*");

 

Find the below working application.

 

relativeElements.html

<html id="html">

<head id="head">
	<title id="title">Relative Elements</title>
	</headname="title">

<body>
	<p id="para1">Paragraph1</p>

	<p id="para2">Paragraph2</p>

	<p id="para3">Paragraph3</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();
		}

	}
}

ElementsRelativeToThisElement.java

package com.sample.app.miscellaneous;

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

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

public class ElementsRelativeToThisElement {

	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("miscellaneous" + File.separator + "relativeElements.html");

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

			ElementHandle elementHandle = page.querySelector("#para1");
			ElementHandle grandParent = elementHandle.querySelector("xpath=../..");
			List<ElementHandle> siblings = elementHandle.querySelectorAll("xpath=following-sibling::*");

			System.out.println("elementHandle : " + elementHandle.getAttribute("id"));
			System.out.println("grandParent : " + grandParent.getAttribute("id"));

			System.out.println("Siblings");
			for (ElementHandle sibling : siblings) {
				System.out.println("\t" + sibling.getAttribute("id"));
			}

		}
	}

}

Output

elementHandle : para1
grandParent : html
Siblings
	para2
	para3

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment