当前位置:网站首页>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)
边栏推荐
- Kotlin basics 1
- 3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
- Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
- [flask] response, session and message flashing
- Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
- Redis' cache penetration, cache breakdown, cache avalanche
- Nmap: network detection tool and security / port scanner
- Leetcode1961. Check whether the string is an array prefix
- 【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
- Leetcode 剑指 Offer 59 - II. 队列的最大值
猜你喜欢
How to upgrade kubernetes in place
[solved] how to generate a beautiful static document description page
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
晶振是如何起振的?
Leetcode skimming questions_ Invert vowels in a string
Unity | two ways to realize facial drive
Loop structure of program (for loop)
Docker compose配置MySQL并实现远程连接
leetcode刷题_平方数之和
一圖看懂!為什麼學校教了你Coding但還是不會的原因...
随机推荐
【Flask】静态文件与模板渲染
关于softmax函数的见解
Format code_ What does formatting code mean
A Cooperative Approach to Particle Swarm Optimization
网易智企逆势进场,游戏工业化有了新可能
ClickOnce does not support request execution level 'requireAdministrator'
记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
MUX VLAN configuration
普通人下场全球贸易,新一轮结构性机会浮出水面
C web page open WinForm exe
How to get all sequences in Oracle database- How can I get all sequences in an Oracle database?
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
基于DVWA的文件上传漏洞测试
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
一图看懂!为什么学校教了你Coding但还是不会的原因...
Maya hollowed out modeling
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
Cookie concept, basic use, principle, details and Chinese transmission