简体   繁体   中英

Selenium WebDriver - Element is not clickable at point, why?

I know there are several threads regarding this topic, but I am not necessarily looking for a solution, but rather an explanation. I work on a very large automation suite that tests a web application via mobile phones using browserstack. My stability is very low.. and it is due to this error getting thrown at me! Occasionally it will work and occasionally it will not.. I can not use Actions because Browserstack does not support that.. WHY does this error exist and has anyone had any success it working around it. I always wait for an object using wait.until(ExpectedConditions), but sometimes this does not work well enough. I cant quite catch it as an exception since it is an Unknown error. Also, our standards do not allow for a Thread.sleep(). Any ideas? Thank you so much

在此处输入图片说明

And here is a screen of some code.. 在此处输入图片说明

You are waiting for a WebElement to be clickable, then again you are finding a list of WebElements and clicking the first element. This does not guarantee that you are clicking the element you waited for it be clickable.

public void waitAndClickElement(WebElement element) {
    driverWait.until(ExpectedConditions.elementToBeClickable(element)).click();
}

In your case,

public void clickImageView() {
    driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click() ;
}

Element is normally not click able due to following reasons .

  1. Html is loading and client is still receiving updates from server
  2. When Scrolling
  3. It can be due to some object is overlapping target

problem 3 can not be resolved you need to fix your code in this wait i wait for HTML to ready and then verify is it click able or not this has eliminated such exceptions from my code

how ever i made a solution for problem 1 and 2 you can simply use my custom wait before clicking . call this function public static void waitForElementPresent(final By by, int timeout,WebDriver driver) After this if you are using browser other then chrome then call Scroll to that object this would fix you problem Code

   public static void waitForElementPresent(final By by, int timeout,WebDriver driver) { 

        waitForPageLoad(driver);
        WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,40).ignoring(StaleElementReferenceException.class); 
        /*  wait.until(new ExpectedCondition<Boolean>(){ 
            @Override 
            public Boolean apply(WebDriver webDriver) { 
              WebElement element = webDriver.findElement(by); 
              return element != null && element.isDisplayed(); 
            } 
          }); */
          try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           wait.until(ExpectedConditions.presenceOfElementLocated(by));
          wait.until(ExpectedConditions.elementToBeClickable(by));
          WebDriverWait wait2 = new WebDriverWait(driver, 40);
          wait2.until(ExpectedConditions.elementToBeClickable(by));

        }
    //wait for page to laod 
    public static void waitForPageLoad(WebDriver driver) {
        ExpectedCondition<Boolean> pageLoadCondition = new
            ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                }
            };
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(pageLoadCondition);
    }

This is due to the speed at which selenium runs. It will try and find elements before the page has loaded, thus resulting in this error.

For the code sample you provided, the .until() returns the WebElement you are waiting for. You can use the code below to click it rather than scraping the page again.

public void clickImageView()
{
    driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM