What Is The Correct Syntax Checking The .readystate Of A Website In Selenium Python?
Solution 1:
Using pageLoadStrategy
as none
and then using WebDriverWait for document.readyState
as interactive
won't be a good approach. You can use either pageLoadStrategy
or WebDriverWait for document.readyState
as follows:
To configure pageLoadStrategy
as None
you can use either of the following solutions:
Firefox :
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities caps = DesiredCapabilities().FIREFOX caps["pageLoadStrategy"] = "none"#caps["pageLoadStrategy"] = "eager" # interactive#caps["pageLoadStrategy"] = "normal" # complete driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe') driver.get("http://google.com")
Chrome :
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities caps = DesiredCapabilities().CHROME caps["pageLoadStrategy"] = "none"#caps["pageLoadStrategy"] = "eager" # interactive#caps["pageLoadStrategy"] = "normal" # complete driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("http://google.com")
Using WebDriverWait to wait for document.readyState
as eager
:
WebDriverWait(driver, 20).until(lambda driver: driver.execute_script("return document.readyState").equals("interactive"))
Using WebDriverWait to wait for document.readyState
as normal
:
WebDriverWait(driver, 20).until(lambda driver: driver.execute_script("return document.readyState").equals("complete"))
You can find a detailed discussion in How to make Selenium not wait till full page load, which has a slow script?
Outro
Do we have any generic function to check if page has completely loaded in Selenium
Solution 2:
You can move the condition into the JS:
WebDriverWait(driver, timeout=20).until(
lambda driver: driver.execute_script('return document.readyState === "interactive"')
)
print(driver.execute_script('return document.readyState'))
If the website is in angular you can use pendingRequests.length === 0
see this answer.
Hope this helps!
Post a Comment for "What Is The Correct Syntax Checking The .readystate Of A Website In Selenium Python?"