Locator class provides ‘elementHandle’, 'elementHandles' methods to get the matching DOM elements.
Signature to get one DOM element
ElementHandle elementHandle()
ElementHandle elementHandle(ElementHandleOptions options)
Get ElementHandle to the matching DOM element. If no elements matching the query are visible, waits for them up to a given timeout. If multiple elements match the selector then it throws an exception.
Example
ElementHandle elementHandle = page.locator("#para3").elementHandle();
List<ElementHandle> elementHandles()
Resolves given locator to all matching DOM elements.
Example
List<ElementHandle> elementHandles = page.locator(".para").elementHandles();
for (ElementHandle elementHandleTemp : elementHandles) {
System.out.println(elementHandleTemp.textContent());
}
Find the below working application.
elementHandle.html
<!DOCTYPE html>
<html>
<head>
<title>inner texts demo</title>
</head>
<body>
<p class="para">Paragraph1</p>
<p class="para">Paragraph2</p>
<p class="para" 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();
}
}
}
GetElementHandle.java
package com.sample.app.locators;
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 GetElementHandle {
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 + "elementHandle.html");
Page page = browser.newPage();
page.setContent(content);
System.out.println("Print one element handle");
ElementHandle elementHandle = page.locator("#para3").elementHandle();
System.out.println(elementHandle.textContent());
System.out.println("\nPrint all the matching element handles");
List<ElementHandle> elementHandles = page.locator(".para").elementHandles();
for (ElementHandle elementHandleTemp : elementHandles) {
System.out.println(elementHandleTemp.textContent());
}
}
}
}
Output
Print one element handle Paragraph3 Print all the matching element handles Paragraph1 Paragraph2 Paragraph3
Previous Next Home
No comments:
Post a Comment