当前位置:网站首页>Selenium eight elements positioning and relative locator
Selenium eight elements positioning and relative locator
2022-07-27 13:44:00 【Si Xiaoyou】
Catalog
''' Eight element positioning rules : id、name、link text、Partial Link Text、Tag Name、Class Name、Xpath、Css Selector xpath And cssSelector The difference in speed is probably 10ms about In the field of automation , Accuracy and stability always rank first . '''
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
1.ID location
# The element has ID When the properties of the , Can pass ID To locate ,ID It's equivalent to the ID number , Generally speaking, it will not be repeated .
driver.find_element("id",'su')
2.Name location
# The element has Name Attribute , adopt Name Attribute to locate . The probability of repetition is relatively possible . Similar to the name on the ID card
# document.getElementsByName("Name name ") according to length Length determines whether there are multiple names Name value
driver.find_element('name','rn')
3.Link Text location
# Aim at a Tag to achieve positioning , The positioning condition is a Labeled text Content
driver.find_element('link text',' Journalism ')
4.Partial Link Text location
# Aim at A Tag to achieve positioning , Is based on a Labeled text Fuzzy search of content . Be similar to sql Medium like
els = driver.find_element("partial link text"," About Baidu ")
# for el in els:
# print(el.text)
print(els.text)
5.TagName location
# Not available for testing , It is usually used by crawlers , Element positioning based on tag name
els = driver.find_elements("tag name","input")
print(len(els))
6.ClassName location
# Based on elements class Property to locate , longer class Attribute values are not recommended , If class It's worth only one , Consider using , But avoid repetition
# driver.find_element('class name','s_ipt').send_keys('s_ipt')
# el = driver.find_element('class name','bg s_ipt_wr new-pmd quickdelete-wrap')
# print(el.get_attribute('class'))
#form > span.bg.s_ipt_wr.new-pmd.quickdelete-wrap
7.CSS Selector location
# Element localization : The element positioning of kaleidoscope . The principle is based on CSS Style to locate elements . It is also the fastest method in legend .
# driver.find_element("css selector","#kw").send_keys("#kw")
8.Xpath Element localization
# Xuzhu personal recommendation commonly used : The panacea of positioning , The principle is based on html Structure to locate elements , Similar to the file management system under the operating system
# Absolute path : It's not the end of the road , Do not use this positioning method
# /html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input
# Relative paths : Element query function , Right click copy,copy xpath Get the relative path of the element ( This method is not recommended ), Usually by hand xpath To solve the problem
# //*[@id="kw"]
# //*[@type="submit"]
e1 = driver.find_element('xpath','//*[@id="kw"]').send_keys(" Si Xiaoyou ")
driver.find_element('xpath','//*[@type="submit"]').click()
9. Relative positioner
''' Position the element relative to the locator , stay selenium4 New content in Locate elements based on the direction of people , Just locate one element , You can locate all the elements around this element to In practice , There will be inaccurate positioning '''
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.baidu.com')
# Core elements
el = driver.find_element("id","kw")
# On
a = driver.find_elements(locate_with(By.TAG_NAME,'div').above(el))
# Next
b = driver.find_elements(locate_with(By.TAG_NAME,'div').below(el))
# Left
l = driver.find_elements(locate_with(By.TAG_NAME,'div').to_left_of(el))
# Right
r = driver.find_elements(locate_with(By.TAG_NAME,'div').to_right_of(el))
# near
n = driver.find_elements(locate_with(By.TAG_NAME,'div').near(el))
print(a)
print(b)
print(l)
print(r)
print(n)
边栏推荐
- eBPF/Ftrace
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- Database kernel developer, worth a mug!!!
- 【C进阶】指针数组 VS 数组指针
- Additional: [urlencoder.encode (string to be encoded, "encoding method");] (what is it?; why do we use this to encode when we set values in cookies?) (to be improved...)
- Construction and application of industrial knowledge atlas (3): commodity knowledge fusion
- Image features and extraction
- 【基础知识】~ 集成电路设计流程,以及各阶段所使用的EDA工具
- MySQL高可用实战方案——MHA
- Interview site: three kinds of questions
猜你喜欢

MFC FTP creates multi-level folders and uploads files to the specified directory of FTP

v-text

52:第五章:开发admin管理服务:5:开发【分页查询admin账号列表,接口】;(Swagger的@ApiParam(),对方法参数进行注释;PageHelper分页插件;拦截器拦截检查登录状态)

纵横靶场-图片的奥秘

Go language series: how to build a go language development environment?

Seata 在蚂蚁国际银行业务的落地实践

Design of network abnormal traffic analysis system

Interface testing practical tutorial 01: interface testing environment construction

Li Hang, director of ByteDance AI Lab: past, present and future of language model

双料第一!
随机推荐
Intranet penetration based on FRP -- SSH Remote connection to intranet server with the help of public server
期货公司开户后续会有哪些服务?
evutil_make_internal_pipe_: pipe: Too many open files
v-text
Image features and extraction
Huiliang technology app is a good place to go to sea: after more than ten years of popularity, why should the United States still choose to go to sea for gold
Set up SSH key based authentication using putty
Leetcode error reporting and its solution
利用eBPF探测Rootkit漏洞
Realize the disk partition and file system mount of the newly added hard disk
Database kernel developer, worth a mug!!!
How to maintain slip ring equipment
2、Citrix Virtual Apps and Desktops 2203剪贴板重定向策略
SCI论文写作
Fiddler bag capturing Tool + night God simulator
JS module, closure application
Creation and destruction of "C language" function stack frame -- (internal skill)
leetcode——83,24;机器学习——神经网络
Double material first!
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering