...
×

Element State with Playwright Python

There are different states for an element, the developer chooses a state according to the product specifications. For example, the Login button on a login page is disabled till the username and password are filled in.

In this article, we will learn about checking different states the element could be in. The below methods can return an accurate state only if the developers are using standard attributes to make them attain the state. If the developer uses custom attributes or CSS to attain the state then try to follow the custom method.

is_checked:

Returns true if the element is checked otherwise false.

page.locator("//input[@type='checkbox']").is_checked()

is_hidden:

Returns true if the element is hidden (not shown in the UI) otherwise false.

page.locator("//input[@type='checkbox']").is_hidden()

is_visible:

Returns true if the element is visible in the UI, visibility means the element should be more than 1 px in size.

page.locator("//input[@type='checkbox']").is_visible()

is_disabled:

The element accepts user actions, if it does not accept(most places grayed out), then the element is disabled. Returns true if the element is disabled

page.locator("//input[@type='checkbox']").is_disabled()

is_enabled:

This is the opposite of is_disabled

page.locator("//input[@type='checkbox']").is_enabled()

is_editable:

If a user can edit a field (text bar, textarea), then the field is enabled, this function returns true if the element is enabled.

page.locator("//input[@type='checkbox']").is_editable()

Custom Method:

If the developer used custom CSS then the standard methods provided by the playwright tool won’t work. In such cases, you need to use the get_attribute function to get the value and proceed.

page.locator("//input[@type='checkbox']").get_attribute()

Author :

Pavankumar Nagaraj is an automation testing expert with over 12 years of experience, specializing in tools like Selenium, Protractor, Puppeteer, and Playwright, and skilled in Java, Python, and JavaScript

Pavankumar Nagaraj
Pavankumar Nagaraj

Pavankumar Nagaraj is an automation testing expert with over 12 years of experience, specializing in tools like Selenium, Protractor, Puppeteer, and Playwright, and skilled in Java, Python, and JavaScript

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.