Check whether element is clickable in selenium

49,466

Solution 1

You can wait for the element to be clickable:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain")

element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "myDynamicElement"))
)

Or, you can follow the EAFP principle and catch an exception being raised by click():

from selenium.common.exceptions import WebDriverException

try:
    element.click()
except WebDriverException:
    print "Element is not clickable"

Solution 2

You can use webElement.isEnabled() and webElement.isDisplayed() methods before performing any operation on the input field...

I hope this will solve you problem...

Else, you can also put a loop to check above 2 conditions and if those are true you can specify the input text and again you can find the same element and you can get the text of the webelement and compare that text with the text you entered. If those matches you can come out of the loop.

Solution 3

to click an (for selenium) unreachable element i alwas use:

public void clickElement(WebElement el) throws InterruptedException {
    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", el);
}
Share:
49,466
Aayush Agrawal
Author by

Aayush Agrawal

Updated on July 09, 2022

Comments

  • Aayush Agrawal
    Aayush Agrawal almost 2 years

    I'm able to verify whether or not an element exists and whether or not it is displayed, but can't seem to find a way to see whether it is 'clickable' (Not talking about disabled).

    Problem is that often when filling a webform, the element i want may be overlayed with a div while it loads. The div itself is rather hard to detect as it's id, name and even the css is flexible. Hence i'm trying to detect whether or not a input field can be 'clicked' or 'filled'. When the overlaying div is present, the field cannot be filled by a regular user (As the div would overlay the input field and not allow the user to fill it) but it can be filled by selenium. I want to prevent that and only allow selenium to fill it once the user can also fill it.