2

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

enter image description here

And here is a screen of some code.. enter image description here

11
  • Could you post some of the failure trace for us? Or any of the code? Commented Oct 19, 2015 at 18:33
  • This may also be a moot point, but are you required to use BrowserStack? Commented Oct 19, 2015 at 18:35
  • @jagdpanzer, I updated the post to give you an example. And currently it is the best solution we have. We are working now on using the selenium grid.. Commented Oct 19, 2015 at 18:38
  • Without seeing it, it sounds like something is overlapping the object you are trying to click (though this assumption could be wildly wrong). Without access to Actions, how are you moving to the element? Is it the driver.findElements(listImageView).get(0).click()? (I'm assuming the .get(0) brings you the the point.) Commented Oct 19, 2015 at 18:45
  • 1
    Thank you all for your comments guys. And yes I do have a driver.manage.timeouts implemented.. I believe you are right when you are saying something is overlapping due to the fact that the application has several moving parts in the mobile version. Is there a way around this instead of adding a thread.sleep()? Commented Oct 19, 2015 at 19:02

4 Answers 4

2

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() ;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I will try this, as it seems to make a lot of sense! Thank you!
1

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);
    }

Comments

0

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.

Comments

0

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();
}

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.