Why do we need to set element waiting 1. Sleep forced waiting Advantages: The code is concise and not verbose 2. Implicitly_Wait() implicit waiting Advantages: Disadvantage: It is necessary to load the entire page before executing the code, which affects the efficiency of code execution. Generally, the desired result is to only load the element I want to locate and execute the code, without waiting for the entire page to be fully loaded before executing the code. 3. WebDriverWait() displays wait . Advantages: Fast code execution efficiency. No need to wait for the entire page to load, just load it onto the element you want to locate to execute the code. It is the most intelligent way to set element waiting. Disadvantages:
The code is as follows.from selenium import webdriver
from time import sleep
dr = webdriver.Chrome()
sleep(2) #set to wait for 2 seconds
dr.get('http://www.baidu.com')
The code is as follows:from selenium import webdriver
from time import sleep
dr = webdriver.Chrome()
dr.implicitly_wait(20) #set to wait for 20 seconds
dr.get('http://www.baidu.com')
dr.find_element_by_id('kw').send_keys('shawn')
dr.find_element_by_id('su').click()
1. The code is concise.
2. Implicitly add to the front of the code_Wait() will be effective throughout the entire program execution process, waiting for the elements to load and not needing to be set every time like sleep<3. If the entire page is not loaded within the set time, NosuchElementError will be reported. If the element is loaded in the 20th second, the following script will be automatically executed and will not wait for 20 seconds.from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait #WebDriverWait pay attention to capitalization
from selenium.webdriver.common.by import By
dr = webdriver.Chrome()
dr.get('http://www.baidu.com')
try:
element = WebDriverWait(dr,10).until(EC.presence_of_element_located((By.ID,'kw')))
element.send_keys('123')
dr.find_element_by_id('su').click()
except Exception as message:
print('element positioning error %s'%message)
finally:
pass
1. To import from selenium. webdriver. support import expected_Conditions as EC
from selenium. webdriver. support. ui import WebDriverWait
from selenium. webdriver. common. by import By
It is necessary to import the above three packages, and the export path is quite complex, verbose, and troublesome
2. Writing code for the wait time is also complex. The steps are a bit too many.
element= WebDriverWait (dr, 10). until (EC. presentationof elementrelocated (By. ID, 'kw'))
element. send_Keys ('123 ').
Related articles
- Record: easycr Error: Could not initialize NNPACK reason Unsupported hardware causing Python program to exit abnormally
- Streamlit installation error resolution
- Matplotlib ImportError: DLL load failed: Unable to find specified module
- Python: can't open file '
': [Errno 22] Invalid argument - Python dictionary application
- Cv2. error: OpenCV (4.5.5) D: a opencv Python opencv Python opencv modules imgproc src imgwarp. cpp: 66
- Python drawing based on f (x) (2D)
- The interval for drawing a histogram in Python must be a saturation integral interval
- PLT.bar Drawing Rules - Taking Drawing Histograms as an Example
- Installed numpy but reported numpy.core.multiarray failed to import
- Regarding the issue of using sqlite to create database tables in PyCharm and not displaying the tables
- After successful installation of Python, the terminal still displays the lower version
- Usage of astype(), iloc, and loc
- Installation of Python's docx library
- Seaborne has installed the correct version, but module 'seaborne' has no attribute appears
- Iloc operates on DataFrame, converting DataFrame and Series to a list, with the maximum and second largest values, and removing punctuation at the end of the string
- AttributeError: module 'pandas' has no attribute' Series'
- Installing pip and PyMySQL in Centos Python 2.7.5 offline environment
- Develop SDK using Python and package Python projects
- The projects of django and celery, nginx and uwsgi protocols, train and queue deep learning tasks through API ports
Latest articles
- Ubuntu decompresses the tar.gz format file
- Ubuntu's XDMCP login to ubuntu desktop crashes
- RunTime Error: cuda out of memory
- Ffmpeg converts a frame from yuv444 format to png format
- Notepad++Theme Change, Highlighting Selected Content
- Record: easycr Error: Could not initialize NNPACK reason Unsupported hardware causing Python program to exit abnormally
- Streamlit installation error resolution
- Resolving Warning for Ignoring Invalid Distribution
- Matplotlib ImportError: DLL load failed: Unable to find specified module
- Python: can't open file '
': [Errno 22] Invalid argument - Executable NotFound: failed to execute 'dot', make sure the Graphviz executors are on your systems'
- Python dictionary application
- Cv2. error: OpenCV (4.5.5) D: a opencv Python opencv Python opencv modules imgproc src imgwarp. cpp: 66
- Python drawing based on f (x) (2D)
- The interval for drawing a histogram in Python must be a saturation integral interval
- PLT.bar Drawing Rules - Taking Drawing Histograms as an Example
- Sort and remove duplicate numbers in C language
- Summary of pedestrian re identification paper
- Unable to boot up when memory is full (Ubuntu)
- Java Spark code error: java. lang. NoSuchMethodError: net. jpuntz. lz4. LZ4BlockInputStream
Hot tags:
java python linux javascript using error android vue.js solution mysql data vue file c++ windows installation configuration c ubuntu method implementation between spring function project spring-boot server system sql database issue c# springboot html docker oracle idea environment command version methods image process resolve uses network excel failed time display