Checking an element's state

Many a time a test fails to click on an element or enter text in a field, as the element is disabled or exists in the DOM but is hidden on the page. This will result in an error being thrown and the test resulting in failure. To build reliable tests that can run unattended, a robust exception and error handling is needed in the test flow.

We can handle these problems by checking the state of elements. The WebElement interface provides the following methods to check the state of an element:

Method

Purpose

isEnabled()

This method checks if an element is enabled. It returns true if enabled, else false if disabled.

isSelected()

This method checks if an element is selected (radio button, checkbox, and so on). It ...

Get Selenium Testing Tools Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.