当前位置:网站首页>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")
边栏推荐
- Graduation thesis project local deployment practice
- Light up the running light, rough notes for beginners (1)
- An article was opened to test the real situation of outsourcing companies
- selenium 元素定位
- 【idea】Could not autowire. No beans of xxx type found
- Concurrent programming - deadlock troubleshooting and handling
- 氫氧化鈉是什麼?
- 【obs】x264编码:“buffer_size“
- Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘
- CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
猜你喜欢

Miracast技术详解(一):Wi-Fi Display

【idea】Could not autowire. No beans of xxx type found

Altimeter data knowledge point 2

1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析

Three body goal management notes

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

Target detection series - detailed explanation of the principle of fast r-cnn

Tshydro tool

C#学习笔记
![[node] NVM version management tool](/img/26/f13a2451c2f177a86bcb2920936468.png)
[node] NVM version management tool
随机推荐
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
When jupyter notebook is encountered, erroe appears in the name and is not output after running, but an empty line of code is added downward, and [] is empty
Basic series of SHEL script (I) variables
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
Oracle code use
IPage能正常显示数据,但是total一直等于0
Target detection series - detailed explanation of the principle of fast r-cnn
Matlab在线性代数中的应用(四):相似矩阵及二次型
Hdu1232 unimpeded project (and collection)
[vscode] recommended plug-ins
Shadowless cloud desktop - online computer
Light up the running light, rough notes for beginners (1)
SOC_ SD_ CMD_ FSM
Ros2 - Service Service (IX)
Basic operation of external interrupt (keil5)
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
Don't confuse the use difference between series / and / *
[node] NVM version management tool
并查集理论讲解和代码实现
SD_ CMD_ SEND_ SHIFT_ REGISTER