当前位置:网站首页>Element positioning of Web Automation
Element positioning of Web Automation
2022-07-05 22:52:00 【FamilyYan】
One 、 Basic element positioning
Locate through an attribute :
Take Baidu browser as an example : Positioning Baidu input box
How to locate elements in the browser :
Press F12, Click on the arrow in the upper left corner , Green , Then click the input box , You can see , There's a piece of code input The label is highlighted , It's located .
You can see inpu label , Yes id class name attribute , These elements can be used for element positioning .
1、 adopt id location
2、 adopt name location
3、 adopt class location
4、 adopt xapth location
5、 adopt css location
Code implementation :
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")
time.sleep(3)
# id Positioning Baidu input box
# res = driver.find_element_by_id("kw")
# print(res)
# class location
# res = driver.find_element_by_class_name('s_ipt')
# print(res)
# name location
# res = driver.find_element_by_name("wd")
# print(res)
# adopt css location
res = driver.find_element_by_css_selector("#kw")
print(res)
res.send_keys(" Winter Olympic Games ")
# Click Baidu
res2 = driver.find_element_by_id("su")
res2.click()
time.sleep(5)
# driver.close()
driver.quit()
Add : How to find css and xPath Relative path and absolute path
On the label of the element that is located Right click -> Copy ->
The difference between the above positioning methods :
Common ground : Only one attribute can be used to locate
Difference :
1、find_element_by_id: adopt id Positioning elements
(1)、id Is the only one. , In general id It's for js With
2、find_element_by_tag_name: Locate the element by tag name
(1)、 Label name is not unique
3、find_element_by_class_name: Locate the element by class name (class')
(1)、class Is not the only
(2)、 If you want to pass class To locate , Make sure you locate this element class It has to be unique
4、find_element_by_name: Through elements name Attribute positioning element ( Elements are required name attribute )
(1)、 The element has to have name attribute
(2)、name Attributes are not necessarily unique
5、find_element_by_link_text:a Tag passed 【 Exactly match 】 Hyperlink text positioning element ( Hyperlink text must be unique , You can't have Spaces )
6、find_element_by_partial_link_text:a Tag passed 【 Fuzzy matching 】 Hyperlink text positioning element ( Hyperlink text must be unique )
Multiple attribute combinations can be used to locate
7、find_element_by_xpath: Locate elements by path
(1)、 Absolute path location 【 no need 】
(2)、 Relative path positioning 【 Common omnipotent 】
Detailed explanation xpath location
Two 、 Locate through the attributes of the element itself
1、 Tag name + Property name
A condition ://input[@class=“s_ipt”]
Multiple conditions ://input[@class=“s_ipt” and @id=“kw”]
Navigate to multiple elements , Take value through index , Index value from 1 Start ://input[@type=“hidden”][2]
2、 Tag name + Text positioning
//a[text()=“ Journalism ”]
3、 Tag name +contains() Function matches partial attribute values or text content positioning
//a[contains(text(),“ Journalism ”)]
By fuzzy matching , It matches to 2 individual :
4、 Tag name + Multiple attribute combination positioning , Any combination is OK , Use logical operators and or
//a[@name=“tj_briicon” or contains(text(),“ more ”)] Locate the 2 individual
//a[@name=“tj_briicon” and contains(text(),“ more ”)] Locate the 1 individual
Or you can write like this :
//a[@name=“tj_briicon” or text() = “ more ”] Locate the 2 individual
//a[@name=“tj_briicon” and text() = “ more ”] Locate the 1 individual
3、 ... and 、 Locate through the relationship between elements ( Axis positioning )
1、 Hierarchical positioning
(1) Locate only direct child nodes : Use one /
eg:
//span[@id=“s_kw_wrap”]/input
(2) Locate all grandchildren :
eg:
//div[@id=“s_fm”]//a
Write this to locate the div There are below 5 individual a label , If you want to locate a specific : You can add attributes
//div[@id=“s_fm”]//a[text()=“ Handwriting ”]
2、 Axis name 1:ancestor: Ancestral node , Include parent nodes
eg:
adopt 3 We can always trust the United Chinese team , look for 1 Melting of ice and snow An appointment to the Winter Olympics
//span[text()=" We can always trust the United Chinese team "]/ancestor::ul/li//span[text()=" Ice and snow as the medium An appointment to the Winter Olympics "]
3、 Axis name 2:parent: Parent node
//span[text()=“ We can always trust the United Chinese team ”]/parent::a/span[text()=“ heat ”]
4、 Axis name 3:following: Current node 【 after 】 All nodes of
//div[@id=“viewClassDetailRoot”]//following::div[@id=“tab-2,8”]
5、 Axis name 4:preceding: Current node 【 Before 】 All nodes of
//div[text()=“ Interactive courseware ”]//preceding::div[@class=“bar-item active”]
6、 Axis name 5:following-sibling: Current node 【 after 】 All the siblings of
eg:
//div[text()=“ test ”]/following-sibling::div[@aria-controls=“pane-7”]
7、 Axis name 6:preceding-sibling: Current node 【 Before 】 All the siblings of
eg:
//div[text()=“ test ”]/preceding-sibling::div[@aria-controls=“pane-1”]
边栏推荐
- Metaverse Ape上线倒计时,推荐活动火爆进行
- Postman核心功能解析-参数化和测试报告
- Opencv judgment points are inside and outside the polygon
- Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
- My experience and summary of the new Zhongtai model
- Metaverse ape received $3.5 million in seed round financing from negentropy capital
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
- Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
- Sparse array [matrix]
- 查看网页最后修改时间方法以及原理简介
猜你喜欢
Tensor attribute statistics
我对新中台模型的一些经验思考总结
Vcomp110.dll download -vcomp110 What if DLL is lost
关于MySQL的30条优化技巧,超实用
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
第一讲:蛇形矩阵
Arduino 测量交流电流
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
一文搞定垃圾回收器
Qtquick3d real time reflection
随机推荐
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
Nacos installation and service registration
Function default parameters, function placeholder parameters, function overloading and precautions
點到直線的距離直線的交點及夾角
The introduction to go language is very simple: String
H5c3 advanced - player
Google Maps case
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Starting from 1.5, build a micro Service Framework -- log tracking traceid
BFC block level formatting context
Metaverse Ape获Negentropy Capital种子轮融资350万美元
Arduino 测量交流电流
Nacos installation and service registration
Why does the C# compiler allow an explicit cast between IEnumerable< T> and TAlmostAnything?
C language - structural basis
How to quickly experience oneos
509. Fibonacci Number. Sol
一文搞定JVM常见工具和优化策略
Activate function and its gradient
Metaverse ape received $3.5 million in seed round financing from negentropy capital