×
Element State Playwright Python

Element State with Playwright Python

Written by


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 :

Senior Software Engineer in Test, with more than a decade of experience in automation tools like Selenium, Playwright, Protractor, and Puppeteer with Python, Java, and JS respectively.