Skip slow loading ULRs in Selenium

Xerox9000 :

I started learning Selenium and i got stuck at some point, i must say i'm quite not sure how to explain the issue i'm having, but i'll make it simple. So let's say we have 5 different URLs, i need to go through them and see if they redirect properly using:

driver.get(*insert URL here*);

Let's give the following URLs:

  1. https://stackoverflow.com/
  2. https://github.com/
  3. https://duckduckgo.com/
  4. https://www.javatpoint.com/
  5. https://www.toolsqa.com/

i have a loop going though driver.get(*insert URL here*);, and i notice that the 4th URL is painfully slow it wont even move to the 5th URL, how do i skip it if it reaches, let's say, 15 sec ?

Here's my java code :

    for(int i=0; i<mediasCAS.length; i++){
        if(mediasCAS[i][4] != "?") {
            driver.get(mediasCAS[i][4]);
        }
        else
            continue;
        System.out.println("Site Name : "+mediasCAS[i][1]);
    }

the mediasCAS is a 2D array that contains the websites informations where the 4th column contains the URLs.

Fenio :

Selenium allows to use method pageLoadTimeout().

From the Javadoc:

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

Combining it with try-catch you can just ignore the exception and continue with for loop like this:

driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
for(int i=0; i<mediasCAS.length; i++){
    try {
        if(mediasCAS[i][4] != "?") {
            driver.get(mediasCAS[i][4]);
        }
        else
            continue;
        System.out.println("Site Name : "+mediasCAS[i][1]);
    } catch (Exception ignore) {
        continue;
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396208&siteId=1