2

In python-selenium there are two ways to find an element.

First, you can use the actual method to find an element, e.g.

element = find_element_by_xpath(myxpath)

Second, you can use the WebDriverWait to make sure the webdriver waits for some timeout until the element has been found in some given state:

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, myxpath)))

For the latter method you can define several 'expected conditions' (see here):

element_located_to_be_selected, element_to_be_clickable, element_to_be_selected etc.

My question: Using only the first method of finding an element, how can I check which state this element is in (in case I found the element). How can I check if is 'clickable', or 'selectable' etc.? Is there an attribute of the element object that I can use to determine the state of the element?

3 Answers 3

4

No, there is no direct method to retrieve the exact state of a WebElement through Selenium.


find_element_by_xpath(xpath)

find_element_by_xpath() would simply find the first matching WebElement using the xpath. If no matching element is found it will raise NoSuchElementException

But Selenium offers multiple methods to validate the state of a WebElement as follows:

  • is_displayed(): Returns a Boolean value (true / false) whether the element is visible to a user or not.

  • is_enabled(): Returns a Boolean value (true / false) whether the element is enabled or not.

  • is_selected(): Returns a Boolean value (true / false) whether the element is selected or not.


WebDriverWait() in-conjunction with expected_conditions class

The canned expected_conditions are generally useful to access the elements when they achieves a definite state as follows:

  • presence_of_element_located(locator) mentions :

    class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
    
    An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
    
  • visibility_of_element_located(locator) mentions :

    class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
    
    An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
    
  • element_to_be_clickable(locator) mentions:

    class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
    
    An Expectation for checking an element is visible and enabled such that you can click it.
    
Sign up to request clarification or add additional context in comments.

Comments

1

There isn't any is_clickable() function or attribute. Looking at the source code

class element_to_be_clickable(object):
    """ An Expectation for checking an element is visible and enabled such that
    you can click it."""
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        element = visibility_of_element_located(self.locator)(driver)
        if element and element.is_enabled():
            return element
        else:
            return False

you can see element_to_be_clickable uses visibility_of_element_located, which waits for visibility using element.is_displayed(), and checks if the element is enabled using element.is_enabled(). You can check the combination of those two.

For element_located_to_be_selected you do have a function, element.is_selected().

Regarding the "click()", it doesn't appear in the Python WebElement API, but in Java's API it stats "There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0", checking those parameters should suffice.

Comments

0

Something like this:

button = driver.find_element_by_class_name('?')
href_data = button.get_attribute('href')
if href_data is None:
is_clickable = False

1 Comment

Not all clickable elements redirect, element can be clickable and perform action without href attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.