简体   繁体   English

Selenium Python | 等待整个页面加载,不依赖于动态元素

[英]Selenium Python | Wait for Entire Page to Load, Not Dependant on a Dynamic Element

I've seen similar questions to this but no one has given me a clear answer on whether or not it is possible for Selenium to know whether the entire page has loaded or not.我见过与此类似的问题,但没有人给我一个明确的答案,说明 Selenium 是否有可能知道整个页面是否已加载。

I know about expected_conditions.visibility_of_element_located((By.TAG_NAME, "div")) but I do not want a dynamic element to look for.我知道expected_conditions.visibility_of_element_located((By.TAG_NAME, "div"))但我不想寻找动态元素。 I also don't want to wait a set number of seconds, I need the program to continue as soon as the entire page is loaded.我也不想等待设定的秒数,我需要程序在整个页面加载后立即继续。

Is this possible?这可能吗?

You can use selenium webdriverWait :您可以使用 selenium webdriverWait

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")

element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )

In the code above, Selenium will wait for a maximum of 10 seconds for an element matching the given criteria to be found.在上面的代码中,Selenium 将等待最多 10 秒以找到匹配给定条件的元素。 If no element is found in that time, a TimeoutException is thrown.如果在那段时间内没有找到任何元素,则抛出TimeoutException

Or you can use the time module:或者您可以使用时间模块:

import time 
time.sleep(10)

In the code above the program will wait for 10 seconds before executing the next line.在上面的代码中,程序将在执行下一行之前等待 10 秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM