Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether element is clickable in selenium

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.

like image 503
Aayush Agrawal Avatar asked Nov 15 '14 08:11

Aayush Agrawal


People also ask

Is not clickable at point Selenium?

Solution 4: Using Actions class in Selenium The exception “Element is not clickable at point” might be thrown when the element is not under focus or the action is being performed on the incorrect WebElement. In such cases, you have to switch to the actual element and perform the click action.

How do I check if a link is enabled in Selenium?

Using web console of your browser inspect element to check if there is any attribute to disable the link using style, class etc. String temp = selenium. getAttribute(locator@attribute); Now you can verify the value of temp.

What is contextClick ()?

Move to Element: contextClick() method first performs mouseMove to the middle of the element location. This function performs the right click at the middle of the web element.


3 Answers

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"
like image 112
alecxe Avatar answered Oct 20 '22 05:10

alecxe


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.

like image 43
MANOR Avatar answered Oct 20 '22 06:10

MANOR


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

public void clickElement(WebElement el) throws InterruptedException {
    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", el);
}
like image 38
wutzebaer Avatar answered Oct 20 '22 05:10

wutzebaer