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:
- https://stackoverflow.com/
- https://github.com/
- https://duckduckgo.com/
- https://www.javatpoint.com/
- 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.
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;
}
}