当前位置:网站首页>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”]
边栏推荐
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- Boring boring
- Thinkphp5.1 cross domain problem solving
- 傅里叶分析概述
- Qtquick3d real time reflection
- Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
- 一文搞定垃圾回收器
- [error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
- The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
- Finally understand what dynamic planning is
猜你喜欢

fibonacci search

audiopolicy

Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
![[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]](/img/f4/4d09dc05f5789b980ebd23cc352f8b.jpg)
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]

Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程

鏈錶之雙指針(快慢指針,先後指針,首尾指針)

Usage Summary of scriptable object in unity

Vcomp110.dll download -vcomp110 What if DLL is lost

The countdown to the launch of metaverse ape is hot

I closed the open source project alinesno cloud service
随机推荐
Distributed resource management and task scheduling framework yarn
Tensor attribute statistics
一文搞定JVM的内存结构
Tiktok__ ac_ signature
我把开源项目alinesno-cloud-service关闭了
C language - structural basis
The introduction to go language is very simple: String
记录几个常见问题(202207)
How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
Un article traite de la microstructure et des instructions de la classe
QT creator 7-cmake update
点到直线的距离直线的交点及夹角
Methods modified by static
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
509. Fibonacci Number. Sol
Starting from 1.5, build a micro Service Framework -- log tracking traceid
openresty ngx_lua请求响应
700. Search in a Binary Search Tree. Sol