How to make Selenium not wait till full page load, which has a slow script?
When Selenium loads a page/url by default it follows a default configuration with pageLoadStrategy
set to normal
. To make Selenium not to wait for full page load we can configure the pageLoadStrategy
. pageLoadStrategy
supports 3 different values as follows:
normal
(full page load)eager
(interactive)none
Here is the code block to configure the pageLoadStrategy
:
Firefox :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().FIREFOX
caps["pageLoadStrategy"] = "normal" # complete
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "none"
driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\path\to\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"] = "normal" # complete
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com")
Note :
pageLoadStrategy
valuesnormal
,eager
andnone
is a requirement as per WebDriver W3C Editor's Draft butpageLoadStrategy
value aseager
is still a WIP (Work In Progress) within ChromeDriver implementation. You can find a detailed discussion in “Eager” Page Load Strategy workaround for Chromedriver Selenium in Python
Don't wait for a page to load using Selenium in Python
ChromeDriver 77.0 (which supports Chrome version 77) now supports eager
as pageLoadStrategy.
Resolved issue 1902: Support eager page load strategy [Pri-2]
As you question mentions of click on elements and scrape data before the page has fully loaded
in this case we can take help of an attribute pageLoadStrategy
. When Selenium loads a page/url by default it follows a default configuration with pageLoadStrategy
set to normal
. Selenium can start executing the next line of code from different Document readiness state
. Currently Selenium supports 3 different Document readiness state
which we can configure through the pageLoadStrategy
as follows:
none
(undefined)eager
(page becomes interactive)normal
(complete page load)
Here is the code block to configure the pageLoadStrategy
:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
caps = DesiredCapabilities().FIREFOX
# caps["pageLoadStrategy"] = "normal" # complete
caps["pageLoadStrategy"] = "eager" # interactive
# caps["pageLoadStrategy"] = "none" # undefined
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://google.com")
How to NOT wait for page load to complete in Selenium by using ChromeDriver? (mobile)
Here is my solution:
private class ChromeOptionsEx: ChromeOptions
{
public override ICapabilities ToCapabilities()
{
var r =(DesiredCapabilities)base.ToCapabilities();
r.SetCapability("pageLoadStrategy","none");
return r;
}
}
Use ChromeOptionsEx instead of ChromeOptions.
These code lines:
chromeOptions.AddAdditionalCapability("webdriver.load.strategy", "none");
chromeOptions.AddAdditionalCapability("pageLoadStrategy", "none");
doesn't work because it will be added to ChromeOptions but pageLoadStategy is not chrome optionts, it is capability. Chrome driver expected t find it in a root of capability.
Explanation:
ChromeOptions generate next map of capabilities:
- {[browserName, chrome]}
- {[version, ]}
- {[platform, ANY]}
- {[chromeOptions,Dictionary2[System.String,System.Object]]}
- {[pageLoadStrategy,none]} <-------- we should add it here and my code do this
where chromeOptions is
- {[args,ReadOnlyCollection1[System.String]]}
- {[binary,]}
- {[androidPackage, com.android.chrome]}
- {[pageLoadStrategy,none]}<-------------------- AddAdditionalCapability add value there it is no ok
How to make selenium Not Wait page loading Just One Time?
I understand what you mean. You would like to change attributes of your driver after it has started. You can try this code.
capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(desired_capabilities=capa)
driver.get(URL)
driver.desired_capabilities.update({'pageLoadStrategy':'normal'})
driver.get(URL)
When i printed the classes attributes, it definitely does change the load strategy while running. Although im not sure if it actually affects the behavior of the browser,since the attributes of the browser are created upon initialization of the driver.
How to make Selenium not wait till full page load, which has a slow script?
When Selenium loads a page/url by default it follows a default configuration with pageLoadStrategy
set to normal
. To make Selenium not to wait for full page load we can configure the pageLoadStrategy
. pageLoadStrategy
supports 3 different values as follows:
normal
(full page load)eager
(interactive)none
Here is the code block to configure the pageLoadStrategy
:
Firefox :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().FIREFOX
caps["pageLoadStrategy"] = "normal" # complete
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "none"
driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\path\to\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"] = "normal" # complete
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com")
Note :
pageLoadStrategy
valuesnormal
,eager
andnone
is a requirement as per WebDriver W3C Editor's Draft butpageLoadStrategy
value aseager
is still a WIP (Work In Progress) within ChromeDriver implementation. You can find a detailed discussion in “Eager” Page Load Strategy workaround for Chromedriver Selenium in Python
Related Topics
How to Round to 2 Decimals with Python
Create Multiple Dataframes in Loop
Distributing Ruby/Python Desktop Apps
How to Print the Full Numpy Array, Without Truncation
Unicodedecodeerror, Invalid Continuation Byte
What Do *Args and **Kwargs Mean
Convert Binary to Ascii and Vice Versa
What Are the Risks of Running 'Sudo Pip'
List Comprehension Rebinds Names Even After Scope of Comprehension. Is This Right
Why Aren't Python Nested Functions Called Closures
How to "Perfectly" Override a Dict
How to Upload File with Python Requests