Search code examples
seleniumwebdriverwaiteager-loadingdesiredcapabilitiespageloadstrategy

Improving loading time of webpage in python via selenium


How to reduce time of explicit wait of a page with full of AJAX statements?
wait=WebDriverWait(driver,timeout=77) gives an exact results of located elements that I need, but consumed time is huge. When I set timeout time to lower than 70 the webdriver can not locate elements gives unable to locate elements.


Solution

  • There are some web pages having some JavaScripts there, making those pages loading long time.
    Selenium has 3 possible settings for the Page loading Strategy. The default strategy is normal. With this setting Selenium will wait for complete document readiness state of the loaded page. To reduce the loading time with Selenium we normally use eager strategy setting. This will wait for interactive document readiness state of the loaded page.
    Here some simple working example of how to set page strategy to eager with Selenium on Python:

    from selenium import webdriver
    from selenium.webdriver import DesiredCapabilities
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    options = Options()
    options.add_argument("start-maximized")
    
    caps = DesiredCapabilities().CHROME
    caps["pageLoadStrategy"] = "eager"
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(service=webdriver_service, options=options, desired_capabilities=caps)