SoFunction
Updated on 2024-11-10

Summary of 3 ways to wait for an element to appear in python

preamble

When doing web or app automation testing, there will be cases where an element is not found and an error is reported.

The code for the lookup has already been executed before it has been loaded, so naturally it won't be able to find the element. Then I can use the wait

The code to find the element is executed after the element is loaded.

There are three ways to wait in Python:

I. Mandatory waiting

Sleep(54)

This method is in the time module and is imported from time import sleep.

For example:

Sleep(10) # means forcibly wait 10s before executing the next line of code

Driver.find_element_by_xpath(“xxxxxx”)

This waiting method executes the next statement when the time is up, but it is rather rigid and does not guarantee that the element has actually been loaded during the waiting time.

II. Hidden waiting

Implicitly_wait(xxx)

This wait indicates that the next step is executed when all the elements of the page are loaded within the specified time, otherwise it waits until the time expires and then continues to the next step.

Driver=()
Driver.implicitly_wait(10)# Wait 10s
(“XXX”)

The disadvantage of this method is that the element you need is already loaded, but the page is not yet loaded, and then you need to continue to wait for the page to finish loading before you can perform the next operation.

Look at the third method, which is more flexible

III. Conspicuous waiting

WebDriverWait, in conjunction with the class's until() and until_not() methods, indicates that the program is going to determine the finger every x seconds.

If the specified element is loaded, then execute the next step, otherwise continue to determine every x seconds, specify the time to intercept the

Stop. If it times out an exception is thrown.

from selenium import webdriver 
from  importWebDriverWait 
from  importexpected_conditions as EC 
from  import By

locator=(,”xxxxxxx”)
d = ()
(“”)
WebDriverWait(d,10,1).unitl(EC.presence_of_element_located(locator))
Print(“XXX”)

Here it means wait for 10s, every 1s to check whether the element appears or not, if it appears, execute the next step until 10s

An exception is thrown if it hasn't appeared after the end.

Attached: python selenium2 in the display wait WebDriverWait and conditional judgment expected_conditions example explanation

#coding=utf-8
from selenium import webdriver
from  import By
from  import expected_conditions as EC
from  import WebDriverWait

base_url = ""
driver = ()
driver.implicitly_wait(5)
'''When both implicit and display waits exist, the timeout is the greater of the two'''
locator = (,'kw')
(base_url)

WebDriverWait(driver,10).until(EC.title_is(u"Baidu, you know."))
'''Judge title, return boolean value'''

WebDriverWait(driver,10).until(EC.title_contains(u"Baidu."))
'''Judge title, return boolean'''

WebDriverWait(driver,10).until(EC.presence_of_element_located((,'kw')))
'''Determine if an element has been added to the dom tree, doesn't necessarily mean the element is visible, returns WebElement if located'''

WebDriverWait(driver,10).until(EC.visibility_of_element_located((,'su')))
'''Determine if an element has been added to the dom and is visible, visible means that the element is displayable and has a width and height greater than 0.'''

WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=,value='kw')))
'''Determine if the element is visible, and return the element if it is visible'''

WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.mnav')))
'''Determine if at least 1 element exists in the dom tree, and return a list if it is located'''

WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,'.mnav')))
'''Determine if at least one element is visible on the page, and return a list if located'''

WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((,"//*[@id='u1']/a[8]"), u'settings')))
'''Determines if the specified element contains the expected string, returns a boolean'''

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

#WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator))
'''Determine if the frame can be switched in, if so, return True and switch in, otherwise return False'''
#Note that there isn't a frame to switch into here #

WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap')))
'''Determine whether an element exists in the dom or is not visible, if visible return False, not visible return this element'''
#note that #swfEveryCookieWrap is a hidden element on this page!

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

#WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(,'su')))
'''Wait for an element to be removed from the dom tree'''
#No suitable example found here

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

WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(,"//*[@id='nr']/option[1]"),True))
'''Determine if the selected state of an element is as expected'''

WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((,"//*[@id='nr']/option[1]"),True))
'''Determine if the selected state of an element is as expected'''
driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click()

instance = WebDriverWait(driver,10).until(EC.alert_is_present())
'''Determine if an alert exists on the page, if so switch to the alert and return the content of the alert'''
print 
()

()

summarize

to this article on python in 3 kinds of waiting for elements to appear on the article is introduced to this, more related python waiting for elements to appear on the content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!