Mu21
Mu21

Reputation: 27

Wait until element is clickable does not timeout correctly

I'm having some problem handling timeouts as it doesn't seem to be working in every situation. I have defined the timeout as follows:

 wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(60))
    .pollingEvery(Duration.ofSeconds(1))
    .ignoring(NoSuchElementException.class);

Now, when I want to wait until an element is present on the page, I use this piece of code :

wait.until(ExpectedConditions.presenceOfElementLocated(locator));

It works in most of the situation (wait for the element and timeout after 60 seconds) but lately we've been having trouble with some of the pages that get stuck loading (there's a message waiting for ... at the bottom left of the page). When this happens, I realize that this piece of code doesn't work properly. It doesn't timeout after 60 seconds but after 10 minutes.

Edit: Actually, trying to investigate my problem a little more, I realized it really comes from another line of code that also contains the wait:

wait.until(ExpectedConditions.elementToBeClickable(locator));

Basically, I click on a link that redirects to another page, wait for a button to be present, wait for the button to be clickable, and click on the button. So it detects the button is present but then it waits for it to be clickable and doesn't time out after 60 seconds.

So when I define my driver, I added the following line:

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

In the console, I see the following line: Timed out receiving message from renderer: 60.000

But how do I catch this exception? I tried to surround my wait with a try/catch but it doesn't work.

try {
wait.until(ExpectedConditions.elementToBeClickable(locator));
} catch (Exception e) {
logger.info("TEST");
throw new TimeoutException("element " + locator.toString() + " not found on the page");
}

How can I do it? Thanks.

Upvotes: 0

Views: 2681

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

You need to take care of a couple of things as follows:

  • Waiting until an element is present on the page ideally won't help much if your usecase is either to get any of the attributes, e.g. classname, innerHTML, etc of an element or to invoke click() on an element.
  • Instead depending on your usecase you need to use either visibilityOfElementLocated() or elementToBeClickable().
  • You can find a couple of detailed discussion in:
  • The reason for the line of code wait.until(ExpectedConditions.presenceOfElementLocated(locator)) doesn't times out after 60 seconds but after 10 minutes may be due to the reason that, along with WebDriverWait you have also configured Implicit Wait and as per the documentation mixing up Implicit Wait and Explicit Wait can cause unpredictable wait times
  • To configure pageLoadTimeout you can use the following code block:

    • Code Block:

      System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      
      driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
      try{
        driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl");
      }catch(WebDriverException e){
        System.out.println("WebDriverException occured");
        }
      driver.quit();
      
    • Console Output:

      INFO: Detected dialect: W3C
      [1563377008.449][SEVERE]: Timed out receiving message from renderer: 1.999
      [1563377008.450][SEVERE]: Timed out receiving message from renderer: -0.001
      [1563377008.461][SEVERE]: Timed out receiving message from renderer: -0.012
      [1563377010.466][SEVERE]: Timed out receiving message from renderer: 1.998
      [1563377010.467][SEVERE]: Timed out receiving message from renderer: -0.001
      [1563377010.476][SEVERE]: Timed out receiving message from renderer: -0.010
      WebDriverException occured
      
  • You can find a relevant detailed discussion in pageLoadTimeout in Selenium not working

Upvotes: 0

Related Questions