I am using this code with ChromeDriver and Chrome browser:
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(20));
Thread.sleep(20000);
driver.quit();
Then webpage keeps loading more than 20 seconds and closing with java.util.concurrent.TimeoutException
But its not working.
CodePudding user response:
The driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(20))
method sets the maximum amount of time the driver should wait for a page load to complete before timing out. This means that if a page takes longer than 20 seconds to load, the driver will throw a TimeoutException
and stop waiting for the page to load. However, this method does not close the browser automatically after 20 seconds, it only sets the page load timeout.
The driver.quit()
method is used to close the browser and end the WebDriver session. But the driver.quit()
method is not affected by the page load timeout and it will close the browser immediately after it is called, regardless of how long the page has been loading.
So, if you want to close the browser after 20 seconds, you need to use the Thread.sleep(20000)
method, which will pause the execution of the program for 20 seconds, before calling the driver.quit()
method.
You could use a combination of both methods, by setting the page load timeout and then waiting for 20 seconds before closing the browser.
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(20));
driver.get("https://www.google.com");
Thread.sleep(20000);
driver.quit();
Alternatively, you could use a Timer and TimerTask to close the browser after a specific time like this:
import java.util.Timer;
import java.util.TimerTask;
...
driver.get("https://www.google.com");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
driver.quit();
timer.cancel();
}
}, 20000);
CodePudding user response:
As per the documentation pageLoadTimeout
Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid argument will be returned.
So pageLoadTimeout is effective when the page is loading is In Progress. If the page loading takes more time TimeoutException is raised.
Example snippet:
public class pageLoadTimeoutJava {
public static void main(String args[])
{
System.setProperty("webdriver.chrome.driver", "C:\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));
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,A;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl");
} catch (TimeoutException e) {
System.out.println("pageLoadTimeout occured");
driver.quit();
}
}
}
CodePudding user response:
You can try this:
import org.openqa.selenium.TimeoutException;
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(20));
try {
driver.get("https://www.amazon.com");
} catch (TimeoutException e) {
// e.printStackTrace();
System.out.println("Quitting...");
driver.quit();
}
CodePudding user response:
Press Ctrl Alt Delete. Click Task manager. Under 'Processes', look for 'Google Chrome' or 'chrome.exe'. Click it, then click End process.