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()
Posts You Might Like
- Why playwright is better than selenium webdriver, is it?
- Handle dropdowns in Playwright Python
- Open the browser and Close in Playwright Python
- Handle checkbox in Playwright Python
- Element Operations in Playwright Python
- Handle IFrames in Playwright Python
- Page level commands in Playwright Python
- Element State with Playwright Python