python+selenium十四:xpath和contains模糊匹配

摘要:
Xpath可以用标签或@any属性定位:例如,它可以用输入标签:driver.find_element_by_Xpath(“//input[@id='kw']”)定位,例如:@type attribute:driver。find_ elements_by_Xpath(“//input[@type='text']”)1.定位Xpath 1.常规属性1.定位驱动程序.find by ID_element_by_Xpath(

xpath可以以标签定位,也可以@任意属性:

如:以input标签定位:driver.find_element_by_xpath("//input[@id='kw']")

如:@type属性:driver.find_elements_by_xpath("//input[@type='text']")

一、xpath定位

1、常规属性

1.通过id定位
driver.find_element_by_xpath("//*[@id='kw']").send_keys("hao")

python+selenium十四:xpath和contains模糊匹配第1张

2.通过tag(标签)定位
*号匹配任何标签:driver.find_element_by_xpath("//*[@id='kw']")

python+selenium十四:xpath和contains模糊匹配第2张

也可以指定标签名称:driver.find_element_by_xpath("//input[@id='kw']")

python+selenium十四:xpath和contains模糊匹配第3张

3.通过class定位
driver.find_element_by_xpath("//input[@class='s_ipt']").send_keys("hao")

python+selenium十四:xpath和contains模糊匹配第4张

4.通过name定位
driver.find_element_by_xpath("//input[@name='wd']").send_keys("hao")

python+selenium十四:xpath和contains模糊匹配第5张

2、其他属性

1.其它属性
driver.find_element_by_xpath("//input[@autocomplete='off']").send_keys("hao")

python+selenium十四:xpath和contains模糊匹配第6张

2.多个属性组合(逻辑运算)
driver.find_elements_by_xpath("//input[@type='text' and @name='wd']")

python+selenium十四:xpath和contains模糊匹配第7张

3.绝对路径:/html/body/xxx/xx[@id=‘kw’]

python+selenium十四:xpath和contains模糊匹配第8张

3、层级关系

python+selenium十四:xpath和contains模糊匹配第9张

1.相对路径:层级关系
driver.find_element_by_xpath("//form[@id='form']/span/input")

python+selenium十四:xpath和contains模糊匹配第10张

如:

/代表绝对路径

python+selenium十四:xpath和contains模糊匹配第11张

//代表相对路径

python+selenium十四:xpath和contains模糊匹配第12张

2.索引:如定位搜索选项框

python+selenium十四:xpath和contains模糊匹配第13张

driver.find_element_by_xpath("//*[@id='nr']/option[3]")

python+selenium十四:xpath和contains模糊匹配第14张

3.同一父级多个子元素

python+selenium十四:xpath和contains模糊匹配第15张

如果同一父级下,有多个相同的子元素,下标从1开始:.//*[@id='u1']/a[2]
也可以这样:.//*[@id='u1']/a[@class="mnav"][1]

4、模糊匹配

1.contains模糊匹配text:contains
如,通过模糊匹配text属性,找到百度首页的“糯米”网站超链接
driver.find_element_by_xpath("//a[contains(text(),'糯')]").click()

python+selenium十四:xpath和contains模糊匹配第16张

2.模糊匹配某个属性:contains
xpath("//input[contains(@id,‘xx')]")
driver.find_element_by_xpath("//input[contains(@class,'s_ip')]").send_keys("hao")

python+selenium十四:xpath和contains模糊匹配第17张

3.模糊匹配以xx开头:starts-with
xpath("//input[starts-with(@id,‘xx') ]")
driver.find_element_by_xpath("//input[starts-with(@class,'s_ip')]").send_keys("hao")

python+selenium十四:xpath和contains模糊匹配第18张

5、文本属性

python+selenium十四:xpath和contains模糊匹配第19张

对于这种文本属性,语法:.//*[text()=‘文本内容’]
除了这个文本属性匹配是.//*[text()=‘文本’]这种格式(无@)
其它的属性,如id,name,class等都是.//*[@id=‘xxx’]  .//*[@name=‘xxx’]这种格式

二、浏览器调试xpath

1.Firefox调试:无firePath的情况下,控制台下输入$x(xpath定位),回车

python+selenium十四:xpath和contains模糊匹配第20张

2.Chrome调试:Console下输入$x(xpath定位),回车

python+selenium十四:xpath和contains模糊匹配第21张

三、table表格定位

1、定位表格

python+selenium十四:xpath和contains模糊匹配第22张

Table表格固定格式:.//*[@id=‘表格id’]/tbody/tr[行数]/td[列数]/a
.//*[@id='bugList']/tbody/tr[6]/td[4]/a

2、参数化行和列
x = 6
y = 4
table = f".//*[@id='bugList']/tbody/tr[{x}]/td[{y}]/a"
driver.find_element_by_xpath(table).click()

python+selenium十四:xpath和contains模糊匹配第23张

3、根据表格标题定位后面的按钮

python+selenium十四:xpath和contains模糊匹配第24张

1.先通过bug的标题名称找到这一行
2.再找到这一行的父节点
3.通过父节点往下搜(编辑按钮都是固定位置)

text = "上传多个附件"
t = f'.//*[text()="{text}"]/../../td[@class="text-right"]/a[@title="编辑"]'
driver.find_element_by_xpath(t).click()

python+selenium十四:xpath和contains模糊匹配第25张

免责声明:文章转载自《python+selenium十四:xpath和contains模糊匹配》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用Proxifier + Fiddler 抓任何包docker “no space left on device”问题定位解决下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JS DOM元素

// 为element增加一个样式名为newClassName的新样式 functionaddClass(element, newClassName) { var value =element.className; element.className = value + " " +newClassName; } var bo...

Element中的Cascader 级联选择器高度不能正常显示如何解决2

新版本的element在使用级联选择器的时候,高度存在问题,如何解决呢,之前的旧版本 可以通过自定义属性popper-class解决,但是新版本的无法使用这个属性解决了,新版本的问题必须在全局的global.css中更改 具体的更改方法如下: 在你的全局global.css里面添加: .el-cascader-menu { height: 300px;...

MD5算法的c++实现

需要注意的几点: (1)md5存取的数据长度仅为64位,位于数据的最前端,大于令其自然溢出。 (2)update函数和final函数处理得很繁琐,需要仔细分析。 (3)16位md5码取32位md5码的中间16位。 1、Md5.hpp #include<cstdio>#include<cstring>#include<algor...

菜鸟python---格式化

"""-----------info----------姓名:年龄:公司:电话:------------end-----------"""name = input("name:")age = input("age:")addr = input("addr:")phone = input("phone:")a = "-----------info------...

Oracle加密解密

Oracle内部有专门的加密包,可以很方便的对内部数据进行加密(encrypt)和解密(decrypt).   介绍加密包之前,先简单说一下Oracle基本数据类型——RAW类型。   RAW,用于保存位串的数据类型,类似于CHAR,声明方式RAW(L),L为长度,以字节为单位,作为数据库列最大2000,作为变量最大32767字节。      操作RAW类...

Element表格eltable多列排序,后端接口排序

ElementUI 2.x版本的 table 组件目前是不支持多列排序的,此方法适用于框架下的table组件多列数据排序,将排序方式以数组形式传递给后端排序。效果如下:   image.png 1.Table组件多列排序 // el-table标签中增加handleSortChange和handleHeaderCellClass方法 //...