V.Anh
V.Anh

Reputation: 535

Selenium explicit wait doesnt work

I have a code that tell Selenium to wait until an element is clickable but for some reason, Selenium doesnt wait but instead, click that element and raise a Not clickable at point (x, y) immediately. Any idea how to fix this ?

x = '//*[@id="arrow-r"]/i'
driver = webdriver.Chrome(path)
driver.get('https://www.inc.com/inc5000/list/2017')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x)))
driver.find_element_by_xpath(x).click()

Upvotes: 0

Views: 510

Answers (2)

cezarypiatek
cezarypiatek

Reputation: 1124

EC.element_to_be_clickable check if element is visible and enabled. In terms of visibility it doesn't cover scenario when element is behind other. Maybe your page use something like blockUI widget and click() occurs before the cover disappears. You can check if element is truly clickable by enriching EC.element_to_be_clickable((By.XPATH, x)) check with assertion that ensure element is not behind by other. In my projects I use implementation as below:

static bool IsElementClickable(this RemoteWebDriver driver, IWebElement element)
{
    return (bool)driver.ExecuteScript(@"
            (function(element){
                var rec = element.getBoundingClientRect();
                var elementAtPosition = document.elementFromPoint(rec.left+rec.width/2, rec.top+rec.height/2);
                return element == elementAtPosition || element.contains(elementAtPosition);
            })(arguments[0]);
    ", element);
}

This code is in C# but I'm sure you can easily translate into your programming language of choice.

UPDATE: I wrote a blog post about problems related to clicking with selenium framework https://cezarypiatek.github.io/post/why-click-with-selenium-so-hard/

Upvotes: 1

Zakaria Shahed
Zakaria Shahed

Reputation: 2707

Here is a link to the 'waiting' section for the Python Selenium docs: Click here

Wait will be like:

element = WebDriverWait(driver, 10).until(
    EC.visibility_of((By.XPATH, "Your xpath"))
)
element.click();

Upvotes: 0

Related Questions