3

In our application, when we try to find a element using

 **Doesn't work** 
   WebDriverWait wait = new WebDriverWait(driver, 10);
   wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='... ']"))

**Doesn't work** 
   WebDriverWait wait = new WebDriverWait(driver, 10);
   wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h3[@class='... ']"));

 **Works**
   Thread.sleep(3000);
   driver.findElement(By.xpath("//h3[@class='... ']");

Element actually appears within 2 seconds.

Why WebDriverWait is not wait and find the element?

Is there a way to wait and find the element without using Thread.sleep but with Selenium wait functions.

Melvin Richard
  • 403
  • 1
  • 7
  • 15
  • Please provide the DOM structure so that the xpath being used can be validated. `class` attribute can be applied to multiple elements. There may be multiple `h3` matching elements. – Zeeshan S. May 08 '17 at 06:18
  • @Melvin Richard How can you conclude that `WebDriverWait` is not functioning? I don't see any action in your code with those elements after you wait for their visibility/presence. Please share more of your code block and relevant HTML DOM. Thanks. – undetected Selenium May 08 '17 at 06:20
  • `WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h3[@class='... ']")); driver.findElement(By.xpath("//h3[@class='... ']");` Have you tried like this ? – NarendraR May 08 '17 at 06:27
  • what do you mean when you say "Doesn't work"? please show us the error message that you are getting. – Breaks Software May 08 '17 at 11:17
  • @ZeeshanSiddiqui : It is not the only element in which the issue happens. It happens for many elements like this. We validate the elements using Firepath and make sure we find it uniquely, then we use write the code. So no issue regarding duplciation – Melvin Richard May 08 '17 at 11:20
  • @BreaksSoftware : Its says element is not clickable at the point. If we use Thread.sleep(2000) or Thread.sleep(2000) , basically making the thread to sleep for 2 or 3 seconds and then trying to click it. It works. But wait.until(ExpectedConditions.presenceOfElementLocated(By.xp‌​ath("//h3[@class='..‌​. ']")).click(); gives element is not clickable at the point issue – Melvin Richard May 08 '17 at 11:22
  • @Dev : Its says element is not clickable at the point. If we use Thread.sleep(2000) or Thread.sleep(2000) , basically making the thread to sleep for 2 or 3 seconds and then trying to click it. It works. But wait.until(ExpectedConditions.presenceOfElementLocated(By.xp‌​ath("//h3[@class='..‌​. ']")).click(); gives element is not clickable at the point issue – Melvin Richard May 08 '17 at 11:26
  • @NarendraRajput : I dint try like below WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.presenceOfElementLocated(By.xp‌​ath("//h3[@class='..‌​. ']")); driver.findElement(By.xpath("//h3[@class='... ']"); – Melvin Richard May 08 '17 at 11:27

3 Answers3

1

Ok, if the error is that "element is not clickable" then WebDriver found the element, but it's not ready to click yet. for your expected condition use:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[@class='... ']"))
Breaks Software
  • 1,721
  • 1
  • 10
  • 15
1

Here is the solution to your Question:

As you are seeing element is not clickable at the point issue I would suggest you the following:

  1. Increase the wait a bit (increase from 10 to 15 sec)
  2. Instead of presenceOfElementLocated clause, use elementToBeClickable clause or elementToBeSelected clause.
  3. Your code will look like:

    WebDriverWait wait1 = new WebDriverWait(driver, 15); 
        wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[@class='‌​..‌​. ']"))); 
    driver.findElement(By.xpath("//h3[@class='... ']")).click();
    

OR

    WebDriverWait wait2 = new WebDriverWait(driver, 15); 
        wait2.until(ExpectedConditions.elementToBeSelected(By.xpath("//h3[@class='‌​..‌​. ']"))); 
    driver.findElement(By.xpath("//h3[@class='... ']")).click();

Let me know if this helps you.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can find different ways for 'wait' in Selenium WebDriver: Fluent wait works as expected, but implicit wait does not

You can try below one which is worked for me.

 new FluentWait<WebDriver>(getDriver())
         .withTimeout(Integer.parseInt(time), TimeUnit.SECONDS)
         .pollingEvery(100, TimeUnit.MILLISECONDS)
         .ignoring(NoSuchElementException.class)
         .ignoring(StaleElementReferenceException.class)
         .until(new ExpectedCondition<WebElement>() {

          public WebElement apply(WebDriver driver) {
           return driver.findElement(locator);
      }
         });
Community
  • 1
  • 1
radhikab
  • 61
  • 3