当前位置:网站首页>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)
边栏推荐
- Basic process and testing idea of interface automation
- [技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
- 竞赛题 2022-6-26
- 【网络攻防实训习题】
- 剑指 Offer 38. 字符串的排列
- 国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
- 3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
- Hcip---ipv6 experiment
- 基于DVWA的文件上传漏洞测试
- SCM Chinese data distribution
猜你喜欢

Huawei Hrbrid interface and VLAN division based on IP

伦敦银走势中的假突破

A Cooperative Approach to Particle Swarm Optimization

Leetcode skimming questions_ Sum of squares

Basic operations of databases and tables ----- non empty constraints

Basic process and testing idea of interface automation

Electrical data | IEEE118 (including wind and solar energy)

C web page open WinForm exe

leetcode刷题_验证回文字符串 Ⅱ

现货白银的一般操作方法
随机推荐
Hcip---ipv6 experiment
Spir - V premier aperçu
yii中console方法调用,yii console定时任务
[solved] how to generate a beautiful static document description page
SPIR-V初窥
Who knows how to modify the data type accuracy of the columns in the database table of Damon
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
MATLB|实时机会约束决策及其在电力系统中的应用
leetcode刷题_反转字符串中的元音字母
How to get the PHP version- How to get the PHP Version?
Folio. Ink is a free, fast and easy-to-use image sharing tool
Internship: unfamiliar annotations involved in the project code and their functions
WGet: command line download tool
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
c#网页打开winform exe
【详细】快速实现对象映射的几种方式
DOM introduction
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
500 lines of code to understand the principle of mecached cache client driver
Redis' cache penetration, cache breakdown, cache avalanche