当前位置:网站首页>Element operation and element waiting in Web Automation
Element operation and element waiting in Web Automation
2022-07-05 22:52:00 【FamilyYan】
One 、 Element operation
1、 Click on
element.click()
2、 Get element name
element.tag_name
from selenium import webdriver
import logging
import time
logging.basicConfig(level=logging.DEBUG)
# Start the browser driver server
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# Find Baidu input box , Enter lemon class
ele = driver.find_element_by_id("kw")
ele.send_keys(" Winter Olympic Games ")
target_name = ele.tag_name
print(" Tag name :", target_name) # Tag name : input
3、 Get element text content
element.text
from selenium import webdriver
import logging
import time
logging.basicConfig(level=logging.DEBUG)
# Start the browser driver server
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# Find search by image
ele = driver.find_element_by_xpath('//span[text()=" Pay attention to ice and snow events Together to the future "]')
text = ele.text
print(" The text is :",text) # The text is : Pay attention to ice and snow events Together to the future
ele.click()
time.sleep(3)
driver.quit()
4、 Clear the input box
element.clear()
from selenium import webdriver
import logging
import time
logging.basicConfig(level=logging.DEBUG)
# Start the browser driver server
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# Find Baidu input box , Enter lemon class
ele = driver.find_element_by_id("kw")
ele.send_keys(" Winter Olympic Games ")
time.sleep(4)
# Emptying operation , When there is a default value , Clear the default value first
ele.clear()
time.sleep(3)
driver.quit()
5、 Get element attribute value
element.get_attribute(name=‘type’)
from selenium import webdriver
import logging
import time
logging.basicConfig(level=logging.DEBUG)
# Start the browser driver server
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# Find Baidu input box
ele = driver.find_element_by_id("kw")
ele.send_keys(" Winter Olympic Games ")
# # Find it, baidu click the button , click
ele2 = driver.find_element_by_id("su")
val = ele2.get_attribute("value")
print(val) # use Baidu Search
ele2.click()
time.sleep(3)
driver.quit()
Two 、 Element to wait
1、 Element waiting for classification
1、 Mandatory waiting
2、 According to wait
3、 An implicit wait
1、 Mandatory waiting
Use time.sleep(5)
2、 An implicit wait
driver.implicitly_wait(2)
characteristic :
(1) The entire session is executed only once , But it works for all elements , That is, all elements will execute element waiting
(2) The waiting time is in seconds
(3) Element wait time exceeded , If the element does not appear , Then the positioning will fail
from selenium import webdriver
import time
# Start the browser driver server
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# An implicit wait
driver.implicitly_wait(2)
# Find Baidu input box
ele = driver.find_element_by_id("kw")
ele.send_keys(" Winter Olympic Games ")
# # Find it, baidu click the button , click
ele2 = driver.find_element_by_id("su")
val = ele2.get_attribute("value")
print(val) # use Baidu Search
ele2.click()
time.sleep(3)
driver.quit()
3、 According to wait
1、 stay timeout Within the set time , And then according to poll_frequency Enter the set time to query whether the element 【 so 、 There is 】
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait Class parameter :
1、driver: Browser driven
2、timeout: Timeout of element waiting ( second ),10
3、poll_frequency=POLL_FREQUENCY: Polling cycle , The default is 0.5 second ,1
4、ignored_exceptions: Exception information thrown after timeout ,no such element exception
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
# Start the browser driver server
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# According to wait
locator = (By.ID, "kw")
wait = WebDriverWait(driver=driver, timeout=10, poll_frequency=1)
# Find Baidu input box
ele = wait.until(EC.visibility_of_element_located(locator=locator))
# ele = driver.find_element_by_id("kw")
ele.send_keys(" Winter Olympic Games ")
# # Find it, baidu click the button , click
ele2 = driver.find_element_by_id("su")
val = ele2.get_attribute("value")
print(val) # use Baidu Search
ele2.click()
time.sleep(3)
driver.quit()
Display waiting for use :
Use
1、 Write separately
# Element positioning expression
locator = (By.ID,"kw")
# Instantiation
wait = WebDriverWait(driver=driver, timeout=10, poll_frequency=1)
# Element localization
ele = wait.until(EC.visibility_of_element_located(locator=locator))
# Wait for multiple elements to be visible
EC.visibility_of_all_elements_located(locator=locator)
2、 Combined writing
ele = WebDriverWait(driver=driver, timeout=10, poll_frequency=1).until(EC.visibility_of_element_located(locator=locator))
3、 ... and 、By Class uses
from selenium.webdriver.common.by import By
ele = driver.find_element_by_id("kw")
ele = driver.find_element(By.ID,"kw")
The two ways of writing are the same
locator = (By.Xpath,"//body//a")
locator = ("xpath","//body//a")
边栏推荐
- Depth first DFS and breadth first BFS -- traversing adjacency tables
- 70. Climbing Stairs. Sol
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
- Ultrasonic sensor flash | LEGO eV3 Teaching
- 一文搞定class的微观结构和指令
- Distance entre les points et les lignes
- GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
- d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
猜你喜欢
Metaverse Ape上线倒计时,推荐活动火爆进行
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
链表之双指针(快慢指针,先后指针,首尾指针)
Distance entre les points et les lignes
Finally understand what dynamic planning is
Codeforces Global Round 19
Depth first DFS and breadth first BFS -- traversing adjacency tables
Metaverse ape received $3.5 million in seed round financing from negentropy capital
一文搞定class的微觀結構和指令
Arduino measures AC current
随机推荐
Methods modified by static
The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
Nanjing: full use of electronic contracts for commercial housing sales
点到直线的距离直线的交点及夹角
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
One article deals with the microstructure and instructions of class
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Un article traite de la microstructure et des instructions de la classe
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
谷歌地图案例
Nangou Gili hard Kai font TTF Download with installation tutorial
Finally understand what dynamic planning is
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
如何快速理解复杂业务,系统思考问题?
Event trigger requirements of the function called by the event trigger
分布式解决方案选型
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
链表之双指针(快慢指针,先后指针,首尾指针)
Go语言学习教程(十五)
I closed the open source project alinesno cloud service