0

My codes where I use WebDriverWait doesn't seem to actually wait. For example

    element = WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.XPATH,"//*[@id='active-grid']/table/thead/tr/th[2]/a[1]"))
    )
    element.click()

clicks before the element is actually clickable and I get element is not clickable at point (74, 297). Other element would receive the click error. It would work if I use time.sleep() and force it to wait a certain amount of seconds for the page to fully load. Problem is the website I need to run this on is super unstable and it would sometime take a second sometimes 30 seconds for each action to load I can't just wait 30 seconds for each step. How do I fix this? What's wrong with the way I'm using webdriverwait? Thank You!

I thought maybe it has something to do with some other element hasn't finished loading and gets in the way? How do I check that? there's a element_to_be_invisible but I don't know how to figure out which element needs to be invisible. Thanks!

Edit: Thanks, it looks like loading screen is blocking it like you guys said I added WebDriverWait(driver,30).until( EC.invisibility_of_element((By.XPATH,"//*[@class='k-loading-image']"))) and it's now working as expected!

2
  • Edit your question and post the full error message. In that error message should be the HTML of the element that is blocking the click. Once you have that, you should be able to create a locator for it and then wait for it to be invisible then do your click. A link to the page would be helpful so we can inspect it ourselves. Commented Mar 29, 2023 at 17:22
  • Also, you are using WebDriverWait correctly. The element you are waiting for IS clickable... it's just blocked by another element but Selenium doesn't know that until you actually attempt the click. Commented Mar 29, 2023 at 17:24

1 Answer 1

0

You can find the element that is blocking the click and create another WebDriverWait and wait for it to be invisible then do your click. The HTML for the blocking element should be in the error message.

Another option is to create a helper click() method that takes care of some of the most common recoverable exceptions,

public static void click(By locator, int timeOutSeconds) throws Exception {
    LocalDateTime now = LocalDateTime.now();
    while (LocalDateTime.now().isBefore(now.plusSeconds(timeOutSeconds))) {
        try {
            new WebDriverWait(driver, Duration.ofSeconds(timeOutSeconds)).until(ExpectedConditions.elementToBeClickable(locator)).click();

            return;
        } catch (ElementClickInterceptedException | StaleElementReferenceException e) {
            // do nothing, loop again
        }
    }
}

If it runs into an ElementClickInterceptedException, it just tries again. This will only recover if the blocking element actually disappears within the timeout. If the blocking element is a popup dialog, then it will still fail and you will have to close the dialog, etc. separately.

Sign up to request clarification or add additional context in comments.

Comments

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.