首页 技术 正文
技术 2022年11月23日
0 收藏 928 点赞 4,191 浏览 2545 个字

爬虫学习之-xpath

1、XPATH使用方法
使用XPATH有如下几种方法定位元素(相比CSS选择器,方法稍微多一点):
a、通过绝对路径定位元素(不推荐!)
WebElement ele = driver.findElement(By.xpath("html/body/div/form/input"));
b、通过相对路径定位元素
WebElement ele = driver.findElement(By.xpath("//input"));
c、使用索引定位元素
WebElement ele = driver.findElement(By.xpath("//input[4]"));
d、使用XPATH及属性值定位元素
WebElement ele = driver.findElement(By.xpath("//input[@id='fuck']"));
//其他方法(看字面意思应该能理解吧)
WebElement ele = driver.findElement(By.xpath("//input[@type='submit'][@name='fuck']"));
WebElement ele = driver.findElement(By.xpath("//input[@type='submit' and @name='fuck']"));
WebElement ele = driver.findElement(By.xpath("//input[@type='submit' or @name='fuck']"));
e、使用XPATH及属性名称定位元素
元素属性类型:@id 、@name、@type、@class、@tittle
//查找所有input标签中含有type属性的元素
WebElement ele = driver.findElement(By.xpath("//input[@type]"));
f、部分属性值匹配
WebElement ele = driver.findElement(By.xpath("//input[start-with(@id,'fuck')]"));//匹配id以fuck开头的元素,id='fuckyou'
WebElement ele = driver.findElement(By.xpath("//input[ends-with(@id,'fuck')]"));//匹配id以fuck结尾的元素,id='youfuck'
WebElement ele = driver.findElement(By.xpath("//input[contains(@id,'fuck')]"));//匹配id中含有fuck的元素,id='youfuckyou'
g、使用任意值来匹配属性及元素
WebElement ele = driver.findElement(By.xpath("//input[@*='fuck']"));//匹配所有input元素中含有属性的值为fuck的元素
元素定位总结//注:本专题只介绍java版
//By id
WebElement ele = driver.findElement(By.id());
//By Name
WebElement ele = driver.findElement(By.id());
//By className
WebElement ele = driver.findElement(By.className());
//By tabName
WebElement ele = driver.findElement(By.tagName());
//By linkText
WebElement ele = driver.findElement(By.linkText());
//By partialLinkText
WebElement ele = driver.findElement(By.partialLinkText());//通过部分文本定位连接
//By cssSelector
WebElement ele = driver.findElement(By.cssSelector());
//By XPATH
WebElement ele = driver.findElement(By.xpath());

爬虫学习之-xpath

=================================栗 子=====================================

1、id  获取id 的属性值

2、starts-with 顾名思义,匹配一个属性开始位置的关键字  — 模糊定位

3、contains 匹配一个属性值中包含的字符串  — 模糊定位

4、text()  函数文本定位

5、last()  函数位置定位

eg

<input id="su" class="bg s_btn btnhover" value="百度一下" type="submit"/>
//*[@id='su'] 获取id 的属性为'su' 的值

//input[contains(@class,'bg s_btn')]
<a class="lb" href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" rel="external nofollow"  name="tj_login" onclick="return false;">登录</a>
//a[starts-with(@name,'tj_lo')] 属性模糊定位
//a[contains(@name,'tj_lo')] 属性模糊定位

爬虫学习之-xpath

<a href="http://www.baidu.com" rel="external nofollow" >百度搜索</a>
//a[text()='百度搜索']

//a[contains(text(),"搜索")] --文本模糊定位

<a id=”setf” href=”//www.baidu.com/cache/sethelp/help.html” onmousedown=”return ns_c({‘fm’:’behs’,’tab’:’favorites’,’pos’:0})” target=”_blank”>把百度设为主页</a>

//a[text()=’把百度设为主页’]

爬虫学习之-xpath

/A/B/C[last()]   表示A元素→B元素→C元素的最后一个子元素,得到id值为e2的E元素
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,953
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,478
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,290
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,107
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,739
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,773