Selenium4自动化测试3--元素定位By.NAME,By.LINK_TEXT 和通过链接部分文本定位,By.PARTIAL_LINK_TEXT,css_selector定位,By.CSS_SELECTOR

4-通过名称定位,By.NAME

name属性为表单中客户端提交数据的标识,一个网页中name值可能不是唯一的。所以要根据实际情况进行判断

复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题
chrome_location = r'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
driver = webdriver.Chrome(options=options)

driver.get('https://www.baidu.com')
# 只获取属性的第一个元素
driver.find_element(By.NAME, 'wd').send_keys("万笑佛博客园")
# 获取属性的所有元素
driver.find_elements(By.NAME, 'wd')[0].send_keys("万笑佛博客园")

time.sleep(3)

5-通过链接文本定位,By.LINK_TEXT 和通过链接部分文本定位,By.PARTIAL_LINK_TEXT

使用链接的全部文字定位元素

复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题
chrome_location = r'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
driver = webdriver.Chrome(options=options)
driver.get('https://www.baidu.com')
driver.find_element(By.LINK_TEXT, '新闻').click()


#部分包含
#driver.find_element(By.PARTIAL_LINK_TEXT, '闻').click()

time.sleep(3)

6-通过css_selector定位,By.CSS_SELECTOR

当一个元素无法直接定位,也就是没有id,name等确定标识,这个时候我们需要考虑使用css selector定位器。

它是一种通过CSS样式选择器来定位元素的方法

CSS常用汇总

复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题
chrome_location = r'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
driver = webdriver.Chrome(options=options)
driver.get('https://www.baidu.com')

#使用id属性定位,id前面要加#号
driver.find_element(By.CSS_SELECTOR, '#kw').send_keys("万笑佛博客园")

#通过class属性定位,class前面要加.
driver.get('https://www.bilibili.com')
driver.find_element(By.CSS_SELECTOR, '.nav-search-input').send_keys('万笑佛博客园')

# 根据name属性定位,属性值为[name="wd"]
driver.get('https://www.baidu.com')
driver.find_element(By.CSS_SELECTOR, '[name="wd"]').send_keys("万笑佛博客园")

# 根据标签属性定位
driver.get('https://www.baidu.com')
driver.find_element(By.CSS_SELECTOR, 'a[href="http://image.baidu.com/"]').click()
# 模糊匹配-包含
driver.find_element(By.CSS_SELECTOR, 'a[href*="image.baidu.com"]').click()
# 模糊匹配-匹配开头
driver.find_element(By.CSS_SELECTOR, 'a[href^="http://image.baidu"]').click()
# 模糊匹配-匹配结尾
driver.find_element(By.CSS_SELECTOR, 'a[href$="image.baidu.com/"]').click()

# 组合定位
driver.get('https://www.baidu.com')
# input+name
driver.find_element(By.CSS_SELECTOR, 'input[name="wd"]').send_keys("万笑佛博客园")
# input+class
driver.find_element(By.CSS_SELECTOR, 'input.s_ipt').send_keys("万笑佛博客园")


# 一般根据最近一个id属性往下找,可以根据class或者标签。
#s-top-left > a
#:nth-child(3)代表第几个子元素,下标从1开始

driver.get('https://www.baidu.com')
# 百度首页新闻,以下三种方式皆可
driver.find_element(By.CSS_SELECTOR, 'div.s-top-left-new.s-isindex-wrap a' ) # 根据class
driver.find_element(By.CSS_SELECTOR, 'div#s-top-left a') # 根据id
driver.find_element(By.CSS_SELECTOR, '#s-top-left a') # 简写
# 百度首页地图,以下2种方式皆可
driver.find_element(By.CSS_SELECTOR, '#s-top-left a:nth-child(3)')
driver.find_elements(By.CSS_SELECTOR, '#s-top-left a')[2]

# a:first-child 第一个标签
driver.find_element(By.CSS_SELECTOR, '#s-top-left a:first-child')
# a:last-child 最后一个标签
driver.find_element(By.CSS_SELECTOR, '#s-top-left a:last-child')




time.sleep(3)
相关推荐
装不满的克莱因瓶2 分钟前
学习使用 Python 机器学习工具 sklearn
人工智能·python·学习·机器学习·ai·agent·智能体
辣椒思密达9 分钟前
Python HTTP请求中的重试与超时控制:提升稳定性的实用方法
开发语言·python·http
Omics Pro1 小时前
3种蛋白结构输入方式!已申报欧洲发明专利
数据库·人工智能·python·机器学习·plotly
Psycho_MrZhang1 小时前
Codex 高效开发协作手册
python
HappyAcmen2 小时前
1.pdfplumber安装,PDF文字提取
python·pdf
弹简特2 小时前
【零基础学Python-收尾】10-Python第三方库的安装介绍
开发语言·python
itfallrain2 小时前
Spring 构造器循环依赖排查:@RequiredArgsConstructor + @Lazy 到底有没有生效
数据库·python·spring
小草cys2 小时前
NVIDIA 驱动(550版本)成功安装后安装支持 GPU 加速的 PyTorch
人工智能·pytorch·python
SilentSamsara2 小时前
Python 微服务全链路:gRPC + 链路追踪 + 服务网格接入
开发语言·分布式·python·微服务·架构
Cloud_Shy6183 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第三章 Item 21 - 24)
开发语言·人工智能·笔记·python·迭代器模式