当前位置:网站首页>Selenium element positioning
Selenium element positioning
2022-07-05 07:24:00 【Live up to your youth】
summary
The most basic unit of a page is the element , Want to locate an element , We need specific information to illustrate the unique characteristics of this element . stay selenium in , Yes 8 A way to locate specific elements . As shown in the following table :
| positioner Locator | describe |
|---|---|
| class | location class The element whose attribute matches the search value ( Compound class names are not allowed ) |
| css selector | location CSS The elements that the selector matches |
| id | location id The element whose attribute matches the search value |
| name | location name The element whose attribute matches the search value |
| link text | location link text The anchor element whose visual text exactly matches the search value |
| partial link text | location link text Anchor element that matches the visual text part with the search value part . If you match multiple elements , Then select only the first element . |
| tag name | Locate the element whose tag name matches the search value |
| xpath | Location and XPath Elements that match the expression |
Method
Suppose our web page consists of the following parts , Make the content of the web page as simple as possible , To introduce the different methods in detail .
<ol id="vegetables">
<li class="onions"><span>Onion is a Vegetable</span></li>
<li class="tomatoes"><span>Tomato is a Vegetable</span></li>
</ol>
<ul id="fruits">
<li class="apples"><span>Apple is a Fruit</span></li>
<li class="tomatoes"><span>Tomato is a Fruit</span></li>
</ul>
according to class Attribute positioning
# Locate in web page class by tomatoes The elements of , There are two qualified web pages , But this method only returns the first
driver.find_element(By.CLASS_NAME, "tomatoes")
# If you need to return all elements , You need to use find_elements Method
driver.find_elements(By.CLASS_NAME, "tomatoes")
according to id Attribute positioning
# stay HTML Kind of , because id The same two elements do not exist , So there is no need for find_elements Method
driver.find_element(By.ID, "fruits")
according to css Selector positioning
# In this positioning mode , More powerful , More flexible positioning , But you need to master css grammar
driver.find_element(By.CSS_SELECTOR, '#fruits .tomatoes')
according to tag name location
# According to the name of the label , This is based on <li></li> Label to locate
driver.find_elements(By.TAG_NAME, "li")
according to name Attribute positioning
name Attributes are generally used in forms , Server pass name Property to get the value of a specific element .
# lookup name The attribute is inp Form for
driver.find_elements(By.NAME, "inp")
according to link text location
This positioning method requires that the content of the element exactly matches the input content .
# The positioning text is Tomato is a Vegetable All elements of
driver.find_elements(By.LINK_TEXT, "Tomato is a Vegetable")
according to partial link text location
This positioning method only needs the content of the element to match the input content to locate the corresponding element .
# The positioning text contains Tomato All elements of
driver.find_elements(By.PARTIAL_LINK_TEXT, "Tomato")
according to xpath location
# according to xpath Expression to locate the element
driver.find_elements(By.XPATH, "/ol/li")
边栏推荐
- [software testing] 05 -- principles of software testing
- Unforgettable summary of 2021
- 苏打粉是什么?
- The difference between NPM install -g/-save/-save-dev
- How to delete the virus of inserting USB flash disk copy of shortcut to
- Executealways of unity is replacing executeineditmode
- docker安装mysql并使用navicat连接
- Simple operation of nixie tube (keil5)
- ImportError: No module named ‘Tkinter‘
- C#学习笔记
猜你喜欢

CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)

一文揭开,测试外包公司的真实情况

What if the DataGrid cannot see the table after connecting to the database

C learning notes

611. 有效三角形的个数

How to deal with excessive memory occupation of idea and Google browser

Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory

SD_ CMD_ RECEIVE_ SHIFT_ REGISTER

U-boot initialization and workflow analysis

Unforgettable summary of 2021
随机推荐
The SQL implementation has multiple records with the same ID, and the latest one is taken
Idea shortcut key
Concurrent programming - deadlock troubleshooting and handling
(tool use) how to make the system automatically match and associate to database fields by importing MySQL from idea and writing SQL statements
苏打粉是什么?
The difference between NPM install -g/-save/-save-dev
SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
一文揭开,测试外包公司的真实情况
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
ImportError: No module named ‘Tkinter‘
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
Basic series of SHEL script (I) variables
C#学习笔记
Powermanagerservice (I) - initialization
Mathematical analysis_ Notes_ Chapter 8: multiple integral
list. files: List the Files in a Directory/Folder
IPage can display data normally, but total is always equal to 0
Netease to B, soft outside, hard in
网易To B,柔外刚中
第 2 章:小试牛刀,实现一个简单的Bean容器