当前位置:网站首页>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 .

边栏推荐
- 浏览器驱动的下载
- Fundamentals of machine learning (II) -- division of training set and test set
- D为何链接不了dll
- 题解《子数整数》、《欢乐地跳》、《开灯》
- Clean up system cache and free memory under Linux
- Use bloc to build a page instance of shutter
- 每日一题:1175.质数排列
- [indomitable medal activity] life goes on and writing goes on
- P3807 [template] Lucas theorem /lucas theorem
- selenium的特点
猜你喜欢

Find love for speed in F1 delta time Grand Prix

2、 Frame mode MPLS operation

你的 Sleep 服务会梦到服务网格外的 bookinfo 吗

Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm

qt中uic的使用

selenium 在pycharm中安装selenium

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner

Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking

De4000h storage installation configuration

SystemServer进程
随机推荐
Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
伙伴云表格强势升级!Pro版,更非凡!
Gee learning notes 2
ArrayList and LinkedList
mysql ---- Oracle中的rownum转换成MySQL
三谈exception——错误处理
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
Chinese name extraction (toy code - accurate head is too small, right to play)
Subcontracting configuration of uniapp applet subpackages
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Astro learning notes
rxjs Observable 自定义 Operator 的开发技巧
Runhe hi3516 development board openharmony small system and standard system burning
Detailed collection of common MySQL commands
Getting started with QT - making a simple calculator
Memory management 01 - link script
OpenApi-Generator:简化RESTful API开发流程
口袋奇兵点评
如何设置Qt手工布局