网站首页 > 技术文章 正文
xpath的定位方法:
(1)full path:是绝对路径 /html/body/div[3]/div[1]/div[5]/div/div/form/span[1]/input
绝对路径方法写法:a./开头, b.从根元素开始
(2)xapath是相对路径://*[@id="kw"]
查看xpath的路径: 选中标签,查看元素,右键 点击copy xpath
//开头 ;
.选取当前的节点
@匹配任意元素节点
*表示当前路径下所有的元素
..选取父级节点
1、xpath中的id定位
from selenium import webdriver #导入selenium第三方库中webdriver
from time import *
dx=webdriver.Chrome() #创建一个对象来来调用谷歌浏览器的对象
dx.get("https://www.baidu.com/") #通过对象打开浏览
dx.find_element_by_xpath('//*[@id="kw"]').send_keys("xpath的id定位")
2、xpath中的name定位
from selenium import webdriver #导入selenium第三方库中webdriver
from time import *
dx=webdriver.Chrome() #创建一个对象来来调用谷歌浏览器的对象
dx.get("https://www.baidu.com/") #通过对象打开浏览
dx.find_element_by_xpath('//*[@name="wd"]').send_keys("xpath的name定位")
3、xpath中的class定位
from selenium import webdriver #导入selenium第三方库中webdriver
from time import *
dx=webdriver.Chrome() #创建一个对象来来调用谷歌浏览器的对象
dx.get("https://www.baidu.com/") #通过对象打开浏览
dx.find_element_by_xpath('//*[@class="s_ipt"]').send_keys("xpath的class定位")
4、xpath中的其他属性定位
from selenium import webdriver #导入selenium第三方库中webdriver
from time import *
dx=webdriver.Chrome() #创建一个对象来来调用谷歌浏览器的对象
dx.get("https://www.baidu.com/") #通过对象打开浏览
dx.find_element_by_xpath('//*[@autocomplete="off"]').send_keys("xpath的其他属性定位")
5、通过and组合属性定位
from selenium import webdriver #导入selenium第三方库中webdriver
from time import *
dx=webdriver.Chrome() #创建一个对象来来调用谷歌浏览器的对象
dx.get("https://www.baidu.com/") #通过对象打开浏览
dx.find_element_by_xpath('//*[@autocomplete="off" and @name="wd"]').send_keys("xpath的组合属性定位")
6、找上一级或上上级定位
from selenium import webdriver #导入selenium第三方库中webdriver
from time import *
dx=webdriver.Chrome() #创建一个对象来来调用谷歌浏览器的对象
dx.get("https://www.baidu.com/") #通过对象打开浏览
dx.find_element_by_xpath('//*[@id="form"]/span[1]/input[1]').send_keys("xpath的组合上一级")
7、上上级定位
from selenium import webdriver #导入selenium第三方库中webdriver
from time import *
dx=webdriver.Chrome() #创建一个对象来来调用谷歌浏览器的对象
dx.get("https://www.baidu.com/") #通过对象打开浏览
dx.find_element_by_xpath('//*[@id="form"]/span[1]/input[1]').send_keys("xpath的组合上一级")
# dx.find_element_by_xpath('//*[@id="form"]/span[1]/input[1]').send_keys("xpath的组合上一级")

猜你喜欢
- 2024-12-29 cgitb,一个非常实用但容易被忽视的宝藏Python库
- 2024-12-29 Python爬虫入门(四):实战,爬取4399小游戏首页
- 2024-12-29 揭秘Python中的超强库:Hypothesis
- 2024-12-29 python+selenium做ui自动化测试用法必会
- 2024-12-29 肝了三天三夜,一文道尽Python的xpath解析
- 2024-12-29 基于opencv的轮廓匹配算法:python代码实现
- 2024-12-29 Python自动化:一键查找文件,告别手动翻找!拯救你的“文件堆”
- 2024-12-29 发掘 Python 的宝藏库:faulthandler,让你的调试轻松无比
- 2024-12-29 业余电台梅登黑德定位系统python计算程序
- 2024-12-29 wxPython - 布局管理简介及绝对位置布局
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)