当前位置:网站首页>selenium 元素定位(2)
selenium 元素定位(2)
2022-07-06 01:33:00 【不负韶华ღ】
概述
selenium 4引入了相对定位器(以前称为友好定位器)。当为所需元素构建定位器并不容易时,这些定位器很有帮助,但很容易在空间上描述元素相对于具有易于构建定位器的元素的位置。
方式
above
locate_with(by: By, using: str).above(element_or_locator:Union[WebElement, Dict] = None)
如果某一元素不容易定位,但是该元素下方的元素容易定位,那么可以先访问该元素下方的元素,再通过above函数访问该元素。
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")
# 定位到标签名为p的元素
element = driver.find_element(By.TAG_NAME, "p")
# 相对定位到该元素上面标签名为h1的元素
locator = locate_with(By.TAG_NAME, "h1").above(element)
# 根据相对定位来查找元素
element = driver.find_element(locator)
below
locate_with(by: By, using: str).below(element_or_locator:Union[WebElement, Dict] = None)
如果某一元素不容易定位,但是该元素上面的元素容易定位,那么可以先访问该元素上面的元素,再通过below函数访问该元素。
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")
# 相对定位到element下面的标签为p元素
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)
如果某一元素不容易定位,但是该元素右边的元素容易定位,那么可以先访问该元素右面的元素,再通过to_left_of函数访问该元素。
right
locate_with(by: By, using: str).to_right_of(element_or_locator:Union[WebElement, Dict] = None)
如果某一元素不容易定位,但是该元素左边的元素容易定位,那么可以先访问该元素左面的元素,再通过to_right_of函数访问该元素。
near
locate_with(by: By, using: str).near(element_or_locator:Union[WebElement, Dict] = None)
如果相对定位不明显,或者根据窗口大小而变化,可以使用 near 方法来识别距离元素最多50px的元素。
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")
# 相对定位到element附近(可以是上下左右)的标签名为p的元素
locator = locate_with(By.TAG_NAME, "p").near(element)
element = driver.find_element(locator)
边栏推荐
- ThreeDPoseTracker项目解析
- How to upgrade kubernetes in place
- What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
- 3D模型格式汇总
- VMware Tools installation error: unable to automatically install vsock driver
- C web page open WinForm exe
- Yii console method call, Yii console scheduled task
- Condition and AQS principle
- 干货!通过软硬件协同设计加速稀疏神经网络
- UE4 unreal engine, editor basic application, usage skills (IV)
猜你喜欢

Basic operations of databases and tables ----- primary key constraints
![[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability](/img/43/a8f302eb69beff4037aadda808f886.png)
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability

Test de vulnérabilité de téléchargement de fichiers basé sur dvwa

Basic operations of databases and tables ----- unique constraints

ORA-00030

Idea sets the default line break for global newly created files

Condition and AQS principle

【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件

Force buckle 1020 Number of enclaves

ThreeDPoseTracker项目解析
随机推荐
Leetcode 剑指 Offer 59 - II. 队列的最大值
Maya hollowed out modeling
[flask] static file and template rendering
SPIR-V初窺
Is chaozhaojin safe? Will it lose its principal
Leetcode 208. Implement trie (prefix tree)
Opinions on softmax function
[Jiudu OJ 09] two points to find student information
[solved] how to generate a beautiful static document description page
c#网页打开winform exe
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
【已解决】如何生成漂亮的静态文档说明页
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
Spir - V premier aperçu
A picture to understand! Why did the school teach you coding but still not
Cookie concept, basic use, principle, details and Chinese transmission
Unity VR resource flash surface in scene
Basic process and testing idea of interface automation
SCM Chinese data distribution
leetcode刷题_反转字符串中的元音字母