The implicit and explicit waits can be used when the page uses AJAX, but I want to stop the loading caused by driver.get()
when sufficient elements are loaded. Is it possible to do so because of the driver.get()
call returns only when the page finishes loading.
Asked
Active
Viewed 2.9k times
29

Ratmir Asanov
- 6,237
- 5
- 26
- 40

ram
- 483
- 1
- 4
- 11
-
Now why would you do such thing, let's say you wait for a button to show. Once the button shows you do whatever you want and it doesn't wait for the page to fully load before it executes your commands unless you got a alterenative reason for this, it's useless. – innicoder Jun 12 '17 at 16:07
-
@ElvirMuslic But the call to driver.get() returns only when the page finishes loading completely. It's like I want to programmatically click that button once the button is visible not waiting for the entire page to load. – ram Jun 12 '17 at 16:22
-
Not really, driver.get() just makes the requests it doesn't Wait for the elements to load. If you say driver.something.click() it will execute it as soon as it gets any response about the page and if the element is not present it will throw an error. Meaning, 1.driver.get() waits for any response from the page (not all). 2. You can execute anything that you'd like and the page doesn't have to be fully loaded but it will load while you're performing those actions. 3. This can be used to save time and usage I guess in some manner (for big projects) – innicoder Jun 12 '17 at 16:33
-
@ElvirMuslic As Florent's answer, the driver.get() doesn't wait for the page to load only when the page load strategy is set to none. – ram Jun 13 '17 at 02:14
1 Answers
59
Yes it's possible by setting the pageLoadStrategy
capability to none
. Then wait for an element to be present and call window.stop
to stop the loading:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(desired_capabilities=capa)
wait = WebDriverWait(driver, 20)
driver.get('http://stackoverflow.com/')
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#h-top-questions')))
driver.execute_script("window.stop();")

Florent B.
- 41,537
- 7
- 86
- 101
-
If I wanted to execute this code locally what would I need to do locally? I understand the dependency requirements but would I need to point to chromedriver within the above test? How would you execute the above without a language binding? – Wunderbread Jun 12 '17 at 19:03
-
1@Wunderbread, I'm not sure what you mean by "without a language binding", but to execute the code locally, either place the chromedriver in the `PATH` environment variable or provide the path to the constructor (`webdriver.Chrome`). – Florent B. Jun 12 '17 at 20:55
-
Got it, thanks. What I meant is executing the above test without a framework e.g. the command "pytest test_foo.py" – Wunderbread Jun 12 '17 at 21:02
-
I am using lower version of Firefox so instead of page load strategy i did profile.setPreference("webdriver.load.strategy", "unstable"); which solved my problem 1 that is get returns even before page loading complete.But I got the problem that finding elemet begins only when page load is complete so I thought of sending escape key via action chains but was not able to do it and window.stop(); didnt work either .I am using firefox 28 and selenium 2.41 – Nimish Bansal Sep 29 '17 at 17:38
-
1for selenium 4 and python one can follow this https://stackoverflow.com/a/71408470/3820418 – Jlearner Mar 09 '22 at 11:42