web 自动化之 Selenium 元素定位和浏览器操作

文章目录

一、元素定位的八大方法

web 自动化测试就是通过代码对网页进行测试,在对网页进行测试之前,必须掌握如何

1、基于 id/name/class/tag_name 定位
2、基于 a 标签元素的链接文本定位
复制代码
import time
from selenium import webdriver

driver=webdriver.Chrome()
driver.get("http://www.baidu.com")
time.sleep(2)
# type: WebElement
el_select=driver.find_element_by_id("kw")
#通过元素的name属性定位
el2=driver.find_element_by_name("wd")
# 通过标签定位 一般情况下 不用这个
el4=driver.find_element_by_tag_name("input")
# class属性定位
el3=driver.find_element_by_class_name("s_ipt")

# 通过链接文本定位
el6=driver.find_element_by_link_text("新闻")
el6.click()
# 通过部分链接文本定位
el7=driver.find_element_by_partial_link_text("新")
el7.click()
# 返回元素列表[]
els=driver.find_elements_by_partial_link_text("新")
3、基于xpath定位
复制代码
# 通过绝对路径定位
import time
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("http://www.baidu.com")
time.sleep(2)
# driver.find_element_by_xpath("xpath语句如何来写")
# 通过绝对路径定位(不要用) 不只是为了定位这个元素,考虑脚本的稳定 通过/从页面开
始标签一直导航到目标标签
el1=driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[5]/div/di
v/form/span[1]/input")
# 通过相对路径定位 //开头 经常用到的方法
el2=driver.find_element_by_xpath("//form/span/input")
# 标签+索引=唯一定位目标标签
el3=driver.find_element_by_xpath("//form/span[1]/input")
# 唯一定位标签+属性
el4=driver.find_element_by_xpath("//form[@id='form']/span[1]/input[@id
='kw']")
# 唯一定位标签+多个属性
el5=driver.find_element_by_xpath("//form[@id='form'and @name='f']/span
[1]/input[@id='kw']")
# 标签+部分属性定位 s_ipt
el6=driver.find_element_by_xpath("//form/span[1]/input[substring(@class,
3)='ipt']")
el7=driver.find_element_by_xpath("//form/span[1]/input[contains(@id,'k
w')]")
el8=driver.find_element_by_xpath("//form/span[1]/input[starts‐with(@i
d,'k')]")
el9=driver.find_element_by_xpath("//div[@id='s‐top‐left']/a[text()='新
闻']")
# 元素操作:输入 send_keys
el7.send_keys("web 自动化测试")
time.sleep(2)
driver.close()
# copyxpath 页面工具复制
el10=driver.find_element_by_xpath('//*[@id="kw"]')
# //标签[属性]
# 通过文本定位
el11=driver.find_element_by_xpath("//a[text()='新闻']")
el6.click()
# xpath定位 =属性+文本+索引 综合使用
# xpath定位能否定位到
4、css定位
复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
# 1通过绝对路径进行定位 一般不用
el1=driver.find_element_by_css_selector("html body div div div div div f
orm span input ")
el2=driver.find_element_by_css_selector("html>body> div> div> div> div >
div> form> span> input ")

# 2通过id定位 #id属性值
# 3通过class属性定位 .class属性值 s_ipt
el3=driver.find_element_by_css_selector("#kw")
el4=driver.find_element_by_css_selector('.s_ipt')

# 4通过其他属性定位,并且多个属性定位
el5=driver.find_element_by_css_selector("[autocomplete='off']")
el6=driver.find_element_by_css_selector("[autocomplete='off'][class='s_i
pt']")

# 5通过标签定位 标签名+属性/id/class进行定位 组合定位
el7=driver.find_element_by_css_selector("input#kw")
el8=driver.find_element_by_css_selector("input.s_ipt")
el9=driver.find_element_by_css_selector("input[autocomplete='off']")

#6通过层级定位 层级之间通过>或者空格隔开 相对路径
el10=driver.find_element_by_css_selector("form#form>span>input#kw")
# 通过兄弟节点定位
# 场景:同一个元素下面多一个相同的元素 多胞兄弟
# 第一个元素 标签:first‐child
# 第二个元素 标签:nth‐child(n)
# 最后元素 标签:last‐of‐type
el11=driver.find_element_by_css_selector("div#s‐top‐left>a:first‐child")
el12=driver.find_element_by_css_selector("div#s‐top‐left>a:nth‐
child(3)")
el13=driver.find_element_by_css_selector("div#s‐top‐left>a:last‐of‐
type")
# el13.click()
# el10.send_keys("chromedriver")
# time.sleep(2)
# driver.close()

"""
定位多个元素
"""
ellist=driver.find_elements_by_css_selector("#kw")
print(ellist)
# 返回WebElement
el14=ellist[0]

"""
元素定位是否通过一个方法,支持所有的定位方式定位到元素
find_element()
find_elements() 基于多个定位方式找到一组元素
"""

el15=driver.find_element(By.CSS_SELECTOR,"#kw")
el16=driver.find_element(By.ID,"kw")
el14.send_keys("chromedriver")
time.sleep(2)
driver.close()
"""
webdriver底层关于元素定位 8+8+2=18
"""

二、浏览器操作

1、信息获取
复制代码
print("浏览器名称",driver.name)
print("网站标题",driver.title)
print("网站地址",driver.current_url)
print("网站源码",driver.page_source)
2、 浏览器关闭
  • python 执行完毕会自动关闭
  • 退出浏览器 driver.quit()
  • 关闭当前窗口 driver.close()
3、 浏览器控制

代码是 driver.xxxxxx

复制代码
driver.maximize_window() #最大化窗口
driver.set_window_size(390, 844) #设置窗口大小
driver.back() #回到空白页面
driver.forward() #前进到上一次访问的页面
driver.refresh() #刷新:地址不变,输入框的内容全部丢失
相关推荐
Cobyte5 分钟前
4.响应式系统基础:从发布订阅模式的角度理解 Vue3 的数据响应式原理
前端·javascript·vue.js
晓得迷路了7 分钟前
栗子前端技术周刊第 124 期 - ESLint v10.2.0、React Native 0.85、Node.js 25.9.0...
前端·javascript·eslint
FJW02081416 分钟前
Kubernetes自动化巡检脚本(Python)
容器·kubernetes·自动化
半个俗人23 分钟前
fiddler的基础使用
前端·测试工具·fiddler
a11177626 分钟前
变电站数字孪生大屏ThreeJS 开源项目
前端·信息可视化·开源·html
恋猫de小郭26 分钟前
AI 的公开测评得分都在作弊,就像泡面的封面,一切以实物为准
前端·人工智能·ai编程
禅思院28 分钟前
使用 VueUse 构建一个支持暂停/重置的 CountUp 组件
前端·vue.js·架构
薛定e的猫咪37 分钟前
2026 年 4 月实测:OpenAI Codex 保姆级教程,从安装到 MCP、Skills 与多智能体协作
前端·数据库·人工智能
I love studying!!!41 分钟前
Web应用程序:用户账户
前端·数据库·sqlite
whuhewei41 分钟前
React性能优化
前端·react.js·性能优化