Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Playwright: Check whether the element is hidden or not

Locator.isHidden method return true when the element is hidden, else false.

 

Signature

boolean isHidden()
boolean isHidden(IsHiddenOptions options)

 

Example

boolean isNameHidden = page.locator("#name").isHidden();

 

Find the below working application.

 

elementHiddenCheck.html


html>

body>

	h1>hidden attribute exampleh1>

	form>
		label for="name">Name:label>
		input type="text" id="name" name="name">br>br>

		label for="age">age:label>
		input type="text" id="age" name="age" disabled>br>br>

		label for="country">Country:label>
		input type="text" id="country" name="country" value="India" readonly>br>br>

		label for="gender">Gender:label>
		input type="text" id="gender" name="gender" value="M" hidden>br>br>

		input type="submit" value="Submit">
	form>

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();
		}

	}
}

ElementHiddenCheck.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;

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 ElementHiddenCheck {
	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 + "elementHiddenCheck.html");

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

			boolean isNameHidden = page.locator("#name").isHidden();
			boolean isAgeHidden = page.locator("#age").isHidden();
			boolean isCountryHidden = page.locator("#country").isHidden();
			boolean isGenderHidden = page.locator("#gender").isHidden();

			System.out.println("isNameHidden : " + isNameHidden);
			System.out.println("isAgeHidden : " + isAgeHidden);
			System.out.println("isCountryHidden : " + isCountryHidden);
			System.out.println("isGenderHidden : " + isGenderHidden);

		}
	}
}

Output

isNameHidden : false
isAgeHidden : false
isCountryHidden : false
isGenderHidden : true

 

Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Playwright: Check whether the element is hidden or not

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×