Assert Attribute Value in Playwright Java

Profile picture for user arilio666

To assert an attribute value in Playwright using Java, you can use the getAttribute method.

Let us get assert the attribute value of item3.

page.navigate("http://autopract.com/selenium/dropdown1/");
        Locator locator = page.locator("select.custom-select option >> nth=-2");
        String attributeV = locator.getAttribute("value");

        if (attributeV.equals("item3")) {
            System.out.println("Attribute value is correct!");
        } else {
            System.out.println("Attribute value is incorrect.");
        }
  • So first we isolated item3 and then with the help of getAttribute we took the attribute value out of it.
  • Now with the help of a simple if else statement we can verify the attribute value of item3.