python-3.xseleniumselenium-webdriverwebdriverpageloadstrategy

How to interact with an element before it is rendered within the HTML DOM through Selenium and Python


Is it possible request an URL and check for the elements before the page renders? I'm using Python + Selenium.


Solution

  • A one word answer to your question will be Yes.

    Explanation

    Generally each WebElement on a webpage have 3 (three) distinct states as follows:

    When Selenium loads a webpage/url by default it follows a default configuration of pageLoadStrategy set to normal. You can opt not to wait for the complete page load. So to avoid waiting for the full webpage to load you can configure the pageLoadStrategy. pageLoadStrategy supports 3 different values as follows:

    1. normal (complete page load)
    2. eager (interactive)
    3. none

    Here is a sample code block to configure the pageLoadStrategy :

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    caps = DesiredCapabilities().FIREFOX.copy()
    caps["pageLoadStrategy"] = "none"
    driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\path\to\geckodriver.exe')
    driver.get("http://google.com")
    

    You can find a detailed discussion in How to make Selenium not wait till full page load, which has a slow script?