当前位置:网站首页>Selenium element positioning (2)
Selenium element positioning (2)
2022-07-06 01:38:00 【Live up to your youth】
summary
selenium 4 Relative locator is introduced ( Formerly known as friendly locator ). When it's not easy to build locators for the required elements , These locators are very helpful , But it's easy to spatially describe the position of elements relative to elements with easy to build locators .
The way
above
locate_with(by: By, using: str).above(element_or_locator:Union[WebElement, Dict] = None)
If an element is not easy to locate , But the element below this element is easy to locate , Then you can access the element below this element first , Re pass above Function to access the element .
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
driver = webdriver.Chrome()
driver.get("http://www.example.com")
# Navigate to the label named p The elements of
element = driver.find_element(By.TAG_NAME, "p")
# Relative positioning to the element, the tag name is h1 The elements of
locator = locate_with(By.TAG_NAME, "h1").above(element)
# Find elements based on relative positioning
element = driver.find_element(locator)
below
locate_with(by: By, using: str).below(element_or_locator:Union[WebElement, Dict] = None)
If an element is not easy to locate , But the elements above this element are easy to locate , Then you can access the element above this element first , Re pass below Function to access the element .
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
driver = webdriver.Chrome()
driver.get("http://www.example.com")
element = driver.find_element(By.TAG_NAME, "h1")
# Relative positioning to element The label below is p Elements
locator = locate_with(By.TAG_NAME, "p").below(element)
element = driver.find_element(locator)
left
locate_with(by: By, using: str).to_left_of(element_or_locator:Union[WebElement, Dict] = None)
If an element is not easy to locate , But the element on the right of this element is easy to locate , Then you can access the element on the right of the element first , Re pass to_left_of Function to access the element .
right
locate_with(by: By, using: str).to_right_of(element_or_locator:Union[WebElement, Dict] = None)
If an element is not easy to locate , But the element on the left of this element is easy to locate , Then you can access the element on the left of the element first , Re pass to_right_of Function to access the element .
near
locate_with(by: By, using: str).near(element_or_locator:Union[WebElement, Dict] = None)
If the relative positioning is not obvious , Or change according to the size of the window , have access to near Method to identify distance elements at most 50px The elements of .
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
driver = webdriver.Chrome()
driver.get("http://www.example.com")
element = driver.find_element(By.TAG_NAME, "h1")
# Relative positioning to element near ( It can be up and down, left and right ) The tag name of the is p The elements of
locator = locate_with(By.TAG_NAME, "p").near(element)
element = driver.find_element(locator)
边栏推荐
- internship:项目代码所涉及陌生注解及其作用
- [flask] official tutorial -part2: Blueprint - view, template, static file
- How does the crystal oscillator vibrate?
- How to see the K-line chart of gold price trend?
- How to upgrade kubernetes in place
- [technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
- Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
- Dynamics 365 开发协作最佳实践思考
- yii中console方法调用,yii console定时任务
- SPIR-V初窺
猜你喜欢
Win10 add file extension
Folio. Ink is a free, fast and easy-to-use image sharing tool
selenium 等待方式
Redis-列表
Leetcode skimming questions_ Sum of squares
Superfluid_ HQ hacked analysis
A picture to understand! Why did the school teach you coding but still not
NiO related knowledge (II)
DOM introduction
Tensorflow customize the whole training process
随机推荐
leetcode刷题_平方数之和
Leetcode skimming questions_ Verify palindrome string II
dried food! Accelerating sparse neural network through hardware and software co design
竞价推广流程
1. Introduction to basic functions of power query
02.Go语言开发环境配置
【全網最全】 |MySQL EXPLAIN 完全解讀
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
【Flask】静态文件与模板渲染
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
037 PHP login, registration, message, personal Center Design
Redis-Key的操作
500 lines of code to understand the principle of mecached cache client driver
Leetcode sword finger offer 59 - ii Maximum value of queue
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
How to see the K-line chart of gold price trend?
MUX VLAN configuration
晶振是如何起振的?
Basic operations of database and table ----- set the fields of the table to be automatically added