当前位置:网站首页>Selenium element positioning method
Selenium element positioning method
2022-07-02 13:51:00 【Acrobat】
Take the input box of Baidu interface as an example :
1、 By element id Attribute for element positioning , stay html Of the elements of id Is the only one. .
Location method :find_element_by_id(id)
2、 By element name Attribute for element positioning , stay html Of the elements of name It's not the only one , It can be repeated . If there are multiple identical attributes , Then the default location is the first .
Location method :find_element_by_name(name)
3. By element class Attribute for element positioning , stay html Of the elements of class It's not the only one , It can be repeated . If there are multiple identical attributes , Then the default location is the first .
Location method :find_element_by_class_name(class_name)------- Be careful , It can only be used here class One of the attribute values .
Look inside class There are two property values , Separated by spaces , We use Only one attribute value can be used when .
4. adopt tag_name location
Locate by the label name of the element , On the same page , There will be many label elements of the same kind , Not recommended . If there are multiple identical attributes , Then the default location is the first .( Because Baidu home page contains many input label , So the following example is only for example )
Location method :find_element_by_tag_name(tag_name)
5. adopt link_text Elements ( Generally used in a label ),link_text The location is hyperlinked All Text content , So the matching condition is absolute .
Location method :find_element_by_text_link
6. adopt partial_link_text To locate ,partial_link_text by link_text A supplement to ,partial_link_text The location is hyperlinked Local Text content , So if the matching condition is partial matching, the condition is satisfied .
Location method :find_element_by_partial_link_text
7. Locate a set of elements , A list will be returned . Then use the element through the subscript of the list , The subscript is from 0 Start calculated .
Location method :find_elements_by_id(id)、find_elements_by_name(name)、find_elements_by_tag_name(tag_name) etc. , In fact, there is one more S.
For example, the following , Through the first input Tag enter user name , Through the second input Tag enter password .
8. Locate by path ,xpath location ( The full path 、 Locate according to attribute value 、 Logic and attribute value positioning 、 Label type and attribute value positioning )
Location method :find_element_by_xpath(xpath)
9.xpath An extension of
Location method :
//*[text()='value']: Locate according to the whole content of the text ----value Specify the entire text content of the bit element .
//*[contains(@attribute,'value')]: Locate according to part of the attribute value ----attribute Is the name of the property ,value Is the value of the property .
//*[starts-with(@attribute,'value')]: Locate according to the beginning of the attribute value ----attribute Is the name of the property ,value Is the value of the property .
Be careful :
If you pass xpath When positioning , Use class When positioning , If class Property has multiple values , Then the attribute value needs to write all the attribute value contents . for example :
10、CSS location ,CSS Positioning is better than xpath Fast positioning ,css Selector syntax is very powerful .
CSS Positioning strategy , Common selectors :
1)id Selectors
expression :#id----# The table looks through the elements id Attribute for element selection ,id What we see is id Property value of property
2)class Selectors
expression :.class----.( spot ) The table looks through the elements class Attribute to select elements ,class Express class Attribute One Property value
3) Element selector
expression :tag_name , Is to select elements by their tag names , Because labels are generally not unique , So I don't recommend .
4) Attribute selector
expression :[attribute='value']---- Select elements by their attributes ,attribute Represents the property name ,value Represents the property value .
Be careful : If you use class attribute , that class You need to fill in all the values
5) Hierarchy selector
Select according to the parent-child relationship of the element
The way 1:element1>element2---- adopt element1 To locate element2, also ,element2 Must be element1 The direct child element of .
The way 2:element1 element2---- adopt element1 To locate element2,element2 Must be element1 The offspring element of , It can not be a direct child element
6)css Expand
input[type^='value']-----input Indicates label name ,type Represents the property name ,^ Beginning of expression ,value Represents the matching content . For example, the content to be matched is P, That means type Attribute to p Elements beginning with letters .
input[type$='value']----input Indicates label name ,type Represents the property name ,$ End of expression ,value Represents the matching content . For example, the content to be matched is d, So that is type Attribute to d Letter ending elements .
input[type*='value']----input Indicates label name ,type Represents the property name ,* Means to contain ,value Represents the matching content . For example, the content to be matched is w, So that is type Property contains w The elements of letters .
Another way to write positioning elements : By importing By Class to call .
driver.find_element_by_id("username").send_keys("admin")
Equate to :driver.find_element(By.ID, 'username').send_keys('admin')
By Class , You can press and hold ctrl Key to jump to the source code view By Class , for example :
Hold down ctrl Key click find_element_by_id, Then jump to the source code : You can view the usage in the source code .
边栏推荐
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- 二、帧模式 MPLS 操作
- Achievements in science and Technology (27)
- A better database client management tool than Navicat
- 【文档树、设置】字体变小
- P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
- Answer: can the audio be set to on by default during easydss video on demand?
- Pocket Raider comments
- When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
- Add sequence number column to query results in MySQL
猜你喜欢
2022零代码/低代码开发白皮书【伙伴云出品】附下载
Don't spend money, spend an hour to build your own blog website
Qt新项目_MyNotepad++
刚好1000粉丝,记录一下
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
Just 1000 fans, record it
What are eNB, EPC and PGW?
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
What are eNB, EPC and PGW?
使用BLoC 构建 Flutter的页面实例
随机推荐
De4000h storage installation configuration
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
D为何链接不了dll
[youcans' image processing learning course] general contents
Daily question: 1175 Prime permutation
2022 zero code / low code development white paper [produced by partner cloud] with download
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
How to explain binary search to my sister? This is really difficult, fan!
P3807 [template] Lucas theorem /lucas theorem
[indomitable medal activity] life goes on and writing goes on
Why can't d link DLL
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
刚好1000粉丝,记录一下
Chinese name extraction (toy code - accurate head is too small, right to play)
二、帧模式 MPLS 操作
题解《子数整数》、《欢乐地跳》、《开灯》
MySQL -- convert rownum in Oracle to MySQL
selenium 在pycharm中安装selenium
Verification failed, please check your call back website. You can follow the instructions
Principle analysis of security rememberme