当前位置:网站首页>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)
边栏推荐
- MCU lightweight system core
- [le plus complet du réseau] | interprétation complète de MySQL explicite
- 电气数据|IEEE118(含风能太阳能)
- [network attack and defense training exercises]
- 伦敦银走势中的假突破
- Redis-Key的操作
- 普通人下场全球贸易,新一轮结构性机会浮出水面
- A Cooperative Approach to Particle Swarm Optimization
- 3D模型格式汇总
- Basic operations of databases and tables ----- unique constraints
猜你喜欢

A picture to understand! Why did the school teach you coding but still not

MATLB|实时机会约束决策及其在电力系统中的应用

Mongodb problem set

Tensorflow customize the whole training process

一图看懂!为什么学校教了你Coding但还是不会的原因...

Force buckle 9 palindromes

Redis-列表
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping

国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!

Ordinary people end up in Global trade, and a new round of structural opportunities emerge
随机推荐
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
一图看懂!为什么学校教了你Coding但还是不会的原因...
D22:indeterminate equation (indefinite equation, translation + problem solution)
leetcode刷题_验证回文字符串 Ⅱ
How does Huawei enable debug and how to make an image port
Unity VR resource flash surface in scene
1. Introduction to basic functions of power query
leetcode-2.回文判断
MCU lightweight system core
【Flask】静态文件与模板渲染
干货!通过软硬件协同设计加速稀疏神经网络
伦敦银走势中的假突破
02.Go语言开发环境配置
Mongodb problem set
TrueType字体文件提取关键信息
False breakthroughs in the trend of London Silver
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
A picture to understand! Why did the school teach you coding but still not
剑指 Offer 38. 字符串的排列
剑指 Offer 12. 矩阵中的路径