refresh until page loads and stop loading page after element present

153 views
Skip to first unread message

Leonard Lee

unread,
Jan 10, 2016, 10:13:11 PM1/10/16
to Selenium Users
Hi, was wondering if anyone knew how to deal with these scenarios:
1. the website you are trying to access is having congestion problems so your driver.get can get errors or take forever to load. how can you keep refreshing until you get a valid page with the element you want to act upon?
2. the page has some external JS or whatever that takes forever to load. the actionable element is ready to be clicked on before driver.get finishes. how can we stop the page load once the element we want is available? I don't want to use a general timeout and I am using phantomjs

lkat...@tripodtech.net

unread,
Jan 11, 2016, 4:28:18 AM1/11/16
to Selenium Users

You need to refresh until the required element is found. Try the below code :

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);  
while(true) {
   try {
       WebElement element = driver.findElement(By);        // Find the element, you required
       if(element.isDisplayed()) {                                       // Check if the required element is present
            break;
      }
  } catch(Exception e) {    
       driver.navigate().refresh();
  }
}

You will come out of the loop, if you find the required element. After this, you can continue with the rest of the flow.


 
Reply all
Reply to author
Forward
0 new messages