How to set page load timeout in selenium?

In Selenium with Python, you can set a page load timeout using the ‘timeouts’ feature provided by the WebDriver. Below are the steps that can be taken to perform the given task.

1. Install Selenium

Firstly, to start with, you need to install Selenium in your machine. To do so, you can use command prompt to run the following command.

pip install selenium

periods

2. Import the necessary modules

Next, install all the required modules in your system.

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import time

3. Create an instance for WebDriver

In the next step, create an instance for the WebDriver which you are going to use. Here, Chrome is chosen.

driver = webdriver.Chrome()
driver.maximize_window()

4. Set the page load timeout

Now, use the ‘set_page_load_timeout’ method to set the maximum time the WebDriver should wait for a page to load completely. In the example, it’s set to 3 seconds (but can be changed).

page_load_timeout = 3
driver.set_page_load_timeout(page_load_timeout)

5. Navigate to a webpage

With the help of try block, navigate to the URL of the webpage.

driver.get("https://www.google.com/")

6. Automate the code

Now, wrap your navigation and automation code within a try-except block to catch the TimeoutException. If the page doesn’t load within the specified timeout, a TimeoutException will be raised.

print(f"Page load exceeded {page_load_timeout} seconds.")

7. Close the WebDriver

Finally, close the WebDriver.

driver.quit()

Here’s an Python example to take you through the complete understanding for the above steps:

Python




from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import time
# create a webdriver instance
driver = webdriver.Chrome()
driver.maximize_window()
# set the page load timeout in seconds
page_load_timeout = 3
driver.set_page_load_timeout(page_load_timeout)
try:
    # navigate to a webpage
    driver.get("https://www.google.com/")
    # your automation code here
except TimeoutException:
    print(f"Page load exceeded {page_load_timeout} seconds.")
else:
    # print the else block if no exception occurs in try block
    print(driver.title)
finally:
    # close the webdriver
    driver.quit()


In the above given example, ‘set_page_load_timeout’ method is first used to set the maximum time for the WebDriver for which it should wait to let the page complete its loading. Then, after, automation is performed with the try-except block to catch the ‘TimeoutException’. If the page doesn’t load within the specific timeout, a ‘TimeoutException’ will be raised. Handling of the page load timeout, as needed, is done in the except block. Along with these, an else block is also added to show if no exception occurs in the try block, and prints the title of the webpage. Ultimately, finally block is used to ensure to close the WebDriver instance to release the resources.

Output:

With exception:

output with exception

As shown above, when the page load exceeds its timeout, output appears as “Page load exceeded 2 seconds”

Without exception:

output without exception

As shown above, when the page load doesn’t exceeds its timeout, then no exception occurs and the output appears as the title of the webpage.

How to set page load timeout in Selenium?

Page load timeouts play a significant role in web development and user experience, acting as a safeguard against slow-loading pages and contributing to the overall success of a website or web application. Developers work diligently to manage and optimize load times to keep users engaged and satisfied. This article focuses on discussing how to set page load timeouts in Selenium.

Similar Reads

What are Selenium Timeouts?

Timeouts in Selenium are the longest periods a WebDriver will wait for specific circumstances or events to happen during a web page interaction. In Selenium, there are many kinds of timeouts:...

Why page load timeout matters?

...

What is Timeout Exception?

...

How page load timeout works in Selenium?

...

How to handle timeout exception in Selenium?

...

How to set page load timeout in selenium?

As talked about page load timeout, it is an important asset which every webpage on a browser requires and needs to have. There are various reasons for this, which are briefly discussed below:...

Conclusion

Now, coming to the timeout exception, it is a type of exception that arises when a program or script runs out of its allotted time to complete a particular task or action. Also, many programming languages and frameworks use this to handle those operations that comparatively take longer time to execute themselves, so that they do not hang for long....

Contact Us