selenium delay function (how to determine whether an element has been loaded into the DOM)

selenium driver has its own delay function, which is divided into explicit waiting and implicit waiting. To sum up briefly:

Explicit wait: it is mainly for an element to be visible, clickable, etc. within the specified time. If it exceeds the time, an exception will be thrown.

Implicit waiting: it is mainly facing the whole page. The next step can be executed only after the whole page is loaded within the specified time. If the time is exceeded, an exception will be thrown.

If explicit waiting and implicit waiting exist at the same time, depending on who has a long time, take whose waiting time.

Development documentation: https://python-selenium-zh.readthedocs.io/zh_CN/latest/5.Waits/

However, this function or class does not directly return a status value, but throws an exception to return the result of whether the element has been loaded into the DOM. It is very painful to use. You need to define your own exception state handling, otherwise the program will suspend the whole thread / process because it throws an exception without specifying the handling method.

Here is an example:

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
from selenium.common.exceptions import TimeoutException


driver = webdriver.Chrome()


driver.get("http://yingyu.xdf.cn/list_907_1.html ") # open the browser to access the website


try:

    WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.conL-box')))


except TimeoutException:             # Capture whether the element cannot be found
    print("Find element timeout")
else:
    # driver.close()
    print('Element found')

WebDriverWait is a delay class. The until method below it can specify various problems that will be encountered when DOM is loaded, such as whether the page title is loaded, whether an ID is loaded, or whether a class is loaded, etc. Please refer to resources for details.

Other examples:

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

base_url = "http://www.baidu.com"
driver = webdriver.Firefox()
driver.implicitly_wait(5)
'''When both implicit waiting and display waiting exist, the timeout time is the larger of the two'''
locator = (By.ID,'kw')
driver.get(base_url)

WebDriverWait(driver,10).until(EC.title_is(u"Baidu once, you will know"))
'''judge title,Returns a Boolean value'''

WebDriverWait(driver,10).until(EC.title_contains(u"use Baidu Search"))
'''judge title,Returns a Boolean value'''

WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))
'''Determine whether an element has been added dom In the tree, it does not mean that the element must be visible. If it is located, it will be returned WebElement'''

WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'su')))
'''Determine whether an element has been added to the dom Inside and visible, visible representative elements can be displayed, and both width and height are greater than 0'''

WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value='kw')))
'''Judge whether the element is visible. If it is visible, return this element'''

WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.mnav')))
'''Determine whether at least one element exists in dom In the tree, if it is located, it returns the list'''

WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,'.mnav')))
'''Judge whether at least one element is visible in the page. If it is located, it will return to the list'''

WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@ id='u1']/a[8]"),u' setting ')
'''Determines whether the specified element contains the expected string and returns a Boolean value'''

WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u' Baidu once ')
'''Determines whether the attribute value of the specified element contains the expected string and returns a Boolean value'''

#WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator))
'''Judge the frame Whether it can be or not? switch If you can, go back True also switch Go in, or go back False'''
#Note that there is no frame that can be switched in

WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap')))
'''Determine whether an element exists in dom Or invisible,Return if visible False,Invisible returns this element'''
#be careful#Swofeverycookeiewrap is a hidden element in this page

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click()
'''Determine whether an element is visible and enable Yes, delegates can click'''
driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click()
#WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click()

#WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su')))
'''Wait for an element from dom Remove from tree'''
#There is no suitable example here

WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]")))
'''Determine whether an element is selected,Generally used in drop-down lists'''

WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"),True))
'''Judge whether the selected state of an element meets the expectation'''

WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id='nr']/option[1]"),True))
'''Judge whether the selected state of an element meets the expectation'''
driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click()

instance = WebDriverWait(driver,10).until(EC.alert_is_present())
'''Determine whether there is on the page alert,If so, switch to alert And return alert Content of'''
print instance.text
instance.accept()

driver.close()

 

expected_ The conditions class provides a method for judging the expected conditions

methodexplain
title_isJudge whether the title of the current page is exactly equal to (= =) the expected string, and return a Boolean value
title_containsJudge whether the title of the current page contains the expected string and return Boolean value
presence_of_element_locatedJudging whether an element is added to the dom tree does not mean that the element must be visible
visibility_of_element_locatedDetermines whether an element is visible Visible means that the element is not hidden, and the width and height of the element are not equal to 0
visibility_ofThe above method does the same thing as the above method, except that the above method needs to be passed into the locator, and this method can be passed directly to the located element
presence_of_all_elements_locatedDetermine whether at least one element exists in the dom tree. For example, if the class of n elements on the page is' column-md-3 ', this method returns True as long as one element exists
text_to_be_present_in_elementDetermines whether the text in an element contains the expected string
text_to_be_present_in_element_valueDetermine whether the value attribute in an element contains the expected string
frame_to_be_available_and_switch_to_itJudge whether the frame can be switched in. If so, return True and switch in. Otherwise, return False
invisibility_of_element_locatedDetermine whether an element does not exist in the dom tree or is invisible
element_to_be_clickableJudge whether an element is visible and enable d. In this case, it is called clickable
staleness_ofWait until an element is removed from the dom tree. Note that this method also returns True or False
element_to_be_selectedIt is generally used to judge whether an element is selected in the drop-down list
element_selection_state_to_beJudge whether the selected state of an element meets the expectation
element_located_selection_state_to_beThe function of the above method is the same as that of the above method, except that the above method passes in the located element, and this method passes in the locator
alert_is_presentDetermine whether alert exists on the page

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

reference material:

Exception handling of Python Selenium

Explicit wait problem

selenium shows waiting and implicit waiting

Keywords: Python

Added by rudibr on Thu, 17 Feb 2022 20:36:08 +0200