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

495 Views Asked by At

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

1

There are 1 best solutions below

0
undetected Selenium On BEST ANSWER

A one word answer to your question will be Yes.

Explanation

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

  • presence: Element is present on the DOM of a page.
  • visibility: Element is present on the DOM of a page and visible.
  • interactable(i.e. clickable): Element is visible and enabled such that you can click it.

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?