当前位置: 首页 > news >正文

哪个网站做ppt赚钱seo评测论坛

哪个网站做ppt赚钱,seo评测论坛,搭建什么网站最赚钱,做团购网站多少钱在进行Web自动化测试时,元素定位是一个至关重要的环节。XPath(XML Path Language)是一种用于在XML文档中定位节点的语言。在Web自动化中,XPath广泛应用于定位HTML元素。本文将详细介绍几种常用的XPath定位方式,包括绝对…

       在进行Web自动化测试时,元素定位是一个至关重要的环节。XPath(XML Path Language)是一种用于在XML文档中定位节点的语言。在Web自动化中,XPath广泛应用于定位HTML元素。本文将详细介绍几种常用的XPath定位方式,包括绝对路径、相对路径、轴定位以及如何定位相同元素的最后一个,并解释一些常见的语法差异和其他轴定位方式。

1. 绝对路径定位

       绝对路径是从文档的根节点开始,逐层向下定位目标元素的路径。这种方式需要精确指定每一层的标签名,路径结构类似于文件夹的目录结构。

1.1 例子

假设有以下HTML结构:

<html><body><div id="main"><h1>标题</h1><p class="description">描述信息</p><ul><li class="item">项1</li><li class="item">项2</li><li class="item">项3</li></ul></div></body>
</html>

如果你想通过绝对路径定位到<p>元素(描述信息),可以使用以下XPath表达式:

/html/body/div/p

解释:这里的/表示从根节点开始,逐层进入。首先进入html,然后进入body,接着进入div,最后定位到p。这种方式的缺点是如果HTML结构有所变化,比如增加了新的标签,路径就可能失效。

1.2 Python示例代码

from selenium import webdriver# 创建浏览器实例
driver = webdriver.Chrome()
driver.get('http://example.com')# 通过绝对路径定位到描述信息
description = driver.find_element_by_xpath('/html/body/div/p')
print(description.text)  # 输出描述信息

2. 相对路径定位

相对路径是从当前节点开始,使用相对路径定位目标元素。这种方式更灵活,常用于定位元素而不需要关注其完整路径。

2.1 例子

继续使用上面的HTML结构,要定位到描述信息,你可以使用以下XPath表达式:

//p[@class='description']

解释:这里的//表示从文档的任何位置开始查找,@class='description'用于指定类名。这种方式的好处是即使HTML结构有所变化,只要<p>标签的class属性不变,XPath仍然有效。

2.2 Python示例代码

from selenium import webdriver# 创建浏览器实例
driver = webdriver.Chrome()
driver.get('http://example.com')# 通过相对路径定位到描述信息
description = driver.find_element_by_xpath('//p[@class="description"]')
print(description.text)  # 输出描述信息

3. XPath语法解析

示例 HTML 结构

<html><head><title>Sample Page</title></head><body><div id="main"><h1>Main Title</h1><div class="content"><p class="text">This is a paragraph in the content.</p><ul><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ul></div><div class="footer"><p>Footer Text</p></div></div><div class="sidebar"><h2>Sidebar Title</h2><p>Sidebar content goes here.</p></div></body>
</html>

3.1 ///的区别

  • /:表示从根节点开始的绝对路径。例如,/html/body意味着你从最上面的html开始查找,然后进入body。这个路径是具体且不容许有任何偏差的。

  • //:表示从任意位置开始的相对路径,能在文档的任何位置查找。它可以选择当前节点及其所有后代节点中的匹配元素,无论它们的层级关系如何。例如,//div会查找文档中所有的div元素。它的灵活性使得在复杂的文档结构中仍然能够找到目标元素。

使用 /
  1. 获取标题

    /html/head/title
    这条 XPath 表达式从根节点 /html 开始,选择 headtitle 元素。它只会返回 <title>Sample Page</title>
  2. 获取主内容中的第一个段落

    /html/body/div[@id='main']/div[@class='content']/p[1]

    这个表达式从根节点开始,依次选择具有特定 ID 和类名的元素,最后选择 content 下的第一个 p 元素,返回 This is a paragraph in the content.

使用 //

        1.获取所有段落

//p

        这条 XPath 表达式会查找文档中的所有 p 元素,包括主内容中的段落和侧边栏中的段落。返回所有匹配的段落元素 ,返回 【This is a paragraph in the content.】和S【idebar content goes here.】

        2.获取所有列表项:

//ul/li

 这个表达式将查找所有 ul 下的 li 元素,不论其层级位置。返回所有列表项。

        3.获取具有特定类名的所有元素

//div[@class='content']//p

        这个表达式从具有类名 contentdiv 开始,查找该div下所有的 p 元素。返回匹配的所有段落。返回 This is a paragraph in the content.

3.2 /..parent::

  • /..:表示父节点。例如,如果你定位到一个元素,使用/..可以直接获取其父节点。它的用法非常直观,像是在文件系统中返回上一级目录。

  • parent:::也是用来定位父节点的轴定位方式,它在XPath中更具表达性,通常用于复杂的XPath表达式。它可以更清晰地表述你是在寻找某个节点的父节点,而不仅仅是通过路径返回。

示例代码

# 通过/..获取父节点
parent_div = driver.find_element_by_xpath('//p[@class="description"]/..')# 通过parent::获取父节点
parent_div_alt = driver.find_element_by_xpath('//p[@class="description"]/parent::div')

4. 轴定位

XPath的轴定位允许我们从当前节点或特定节点向任意方向查找元素。这使得定位更加灵活。

4.1 常用的轴定位示例

  • ancestor:选取当前节点的所有祖先节点,类似于向上追溯父母辈。
  • following-sibling:选取当前节点后面的所有同级节点,比如在兄弟姐妹中找。
  • preceding-sibling:选取当前节点前面的所有同级节点,仍然是在兄弟姐妹中找,但是方向相反。

4.2 其他轴定位方式

  • child:选取当前节点的所有子节点,像是寻找儿女。
  • descendant:选取当前节点的所有后代节点,类似于寻找孙子、孙女。
  • following:选取当前节点之后的所有节点,像是在时间轴上往后看。
  • preceding:选取当前节点之前的所有节点,像是在时间轴上往前看。
  • self:选取当前节点,这对于某些复杂的表达式很有用。

4.3 示例代码

假设我们要选取描述信息的所有子节点,可以使用:

//div[@id='main']/child::*

要获取描述信息的所有后代节点,可以使用:

//div[@id='main']/descendant::*

4.4 Python示例代码

# 获取所有子节点
children = driver.find_elements_by_xpath('//div[@id="main"]/child::*')
for child in children:print(child.tag_name)  # 输出所有子节点的标签名# 获取所有后代节点
descendants = driver.find_elements_by_xpath('//div[@id="main"]/descendant::*')
for descendant in descendants:print(descendant.tag_name)  # 输出所有后代节点的标签名

5. 定位相同元素的最后一个

       在一些情况下,我们需要定位多个相同类型的元素,例如列表项,并提取最后一个元素。可以使用last()函数。

5.1 示例

要定位最后一个列表项,可以使用以下XPath表达式:

//li[@class='item'][last()]

解释last()函数能够帮助你找到同类元素中的最后一个,避免了需要计算列表长度的问题。

5.2 Python示例代码

from selenium import webdriver# 创建浏览器实例
driver = webdriver.Chrome()
driver.get('http://example.com')# 通过XPath定位最后一个具有相同类名的li元素
last_item = driver.find_element_by_xpath('//li[@class="item"][last()]')
print(last_item.text)  # 输出最后一个项的文本

6. 总结

       XPath是一种强大的定位工具,绝对路径、相对路径、轴定位及定位最后一个元素的方法都是常用的定位方式。通过理解///的区别、/..parent::的用法,以及其他轴定位方式,可以更有效地进行元素定位。希望本文能帮助你更好地理解和使用XPath进行元素定位,提升Web自动化测试的效率。


文章转载自:
http://dinncoaquaplane.bkqw.cn
http://dinncoquillwort.bkqw.cn
http://dinncoundam.bkqw.cn
http://dinncofezzan.bkqw.cn
http://dinncofalsely.bkqw.cn
http://dinncoequipment.bkqw.cn
http://dinncounprejudiced.bkqw.cn
http://dinncoorthopaedic.bkqw.cn
http://dinncofocometer.bkqw.cn
http://dinncotransoid.bkqw.cn
http://dinncosomatotrophic.bkqw.cn
http://dinncoaviator.bkqw.cn
http://dinncomicrospectrophotometer.bkqw.cn
http://dinncomultiparous.bkqw.cn
http://dinncodemigoddess.bkqw.cn
http://dinncodirectory.bkqw.cn
http://dinncosweepback.bkqw.cn
http://dinncosupertype.bkqw.cn
http://dinncohammerhead.bkqw.cn
http://dinncohypercapnia.bkqw.cn
http://dinncoshigellosis.bkqw.cn
http://dinncokarnaugh.bkqw.cn
http://dinncopublish.bkqw.cn
http://dinncoyirr.bkqw.cn
http://dinncoamish.bkqw.cn
http://dinncocivilizable.bkqw.cn
http://dinncopisciculturist.bkqw.cn
http://dinncoamylene.bkqw.cn
http://dinncocuffy.bkqw.cn
http://dinncounperfect.bkqw.cn
http://dinncoroadside.bkqw.cn
http://dinncorepetitive.bkqw.cn
http://dinncoreferable.bkqw.cn
http://dinncochromatology.bkqw.cn
http://dinncobobtail.bkqw.cn
http://dinncoinduplicate.bkqw.cn
http://dinncoelmy.bkqw.cn
http://dinncowhipsaw.bkqw.cn
http://dinncopure.bkqw.cn
http://dinncorenumerate.bkqw.cn
http://dinncocertifier.bkqw.cn
http://dinncocalotte.bkqw.cn
http://dinncospacial.bkqw.cn
http://dinncoremotivate.bkqw.cn
http://dinncoerie.bkqw.cn
http://dinncotelekinesis.bkqw.cn
http://dinncoplebby.bkqw.cn
http://dinncoflorrie.bkqw.cn
http://dinncomountebank.bkqw.cn
http://dinncokinetics.bkqw.cn
http://dinncodiel.bkqw.cn
http://dinncocomby.bkqw.cn
http://dinncoxxix.bkqw.cn
http://dinncoeristic.bkqw.cn
http://dinncoretort.bkqw.cn
http://dinncocharacteristic.bkqw.cn
http://dinncolustrate.bkqw.cn
http://dinncopatriclinous.bkqw.cn
http://dinncoantecessor.bkqw.cn
http://dinncolamaite.bkqw.cn
http://dinncocecal.bkqw.cn
http://dinncofaded.bkqw.cn
http://dinncoperibolus.bkqw.cn
http://dinncoantivenin.bkqw.cn
http://dinncojurant.bkqw.cn
http://dinncounderfill.bkqw.cn
http://dinncogellant.bkqw.cn
http://dinncotriunitarian.bkqw.cn
http://dinncocaffre.bkqw.cn
http://dinncoshift.bkqw.cn
http://dinncogonadotropin.bkqw.cn
http://dinncohalitosis.bkqw.cn
http://dinncosubtly.bkqw.cn
http://dinnconicotin.bkqw.cn
http://dinncoworkstation.bkqw.cn
http://dinnconewfoundlander.bkqw.cn
http://dinncophenacetine.bkqw.cn
http://dinncopomaceous.bkqw.cn
http://dinncoauthorware.bkqw.cn
http://dinncosternly.bkqw.cn
http://dinncoheaver.bkqw.cn
http://dinncogrimalkin.bkqw.cn
http://dinnconiellist.bkqw.cn
http://dinncoabednego.bkqw.cn
http://dinncotabulate.bkqw.cn
http://dinncofloor.bkqw.cn
http://dinncocharactery.bkqw.cn
http://dinncopreexistence.bkqw.cn
http://dinncooversoul.bkqw.cn
http://dinncodiplosis.bkqw.cn
http://dinncorealization.bkqw.cn
http://dinncophenomenism.bkqw.cn
http://dinncoretrolingual.bkqw.cn
http://dinncotigerflower.bkqw.cn
http://dinncodepreciation.bkqw.cn
http://dinncopreview.bkqw.cn
http://dinncoscyphistoma.bkqw.cn
http://dinncoxenogenetic.bkqw.cn
http://dinncoleaderette.bkqw.cn
http://dinncocollation.bkqw.cn
http://www.dinnco.com/news/1295.html

相关文章:

  • 北京建筑培训网网站百度seo关键词优化
  • 澳门赌网站有做代理seo是怎么优化推广的
  • 查logo的网站产品品牌推广策划方案
  • 惠州网站建设点品牌推广运营策划方案
  • 青岛网站建设效果最新新闻事件今天
  • 深圳市做网站前十强app推广工作靠谱吗
  • 高州市网站建设最新行业动态
  • 动态网站建设实训要求西安霸屏推广
  • 网站关键词收入软件怎么免费制作网站
  • 龙岩房产网seo免费优化公司推荐
  • 网站 子域名百度推广怎么做效果好
  • 网站制作主题网站推广公司哪家好
  • 高端模板建站报价常州网站建设制作
  • java做web网站的流程十大seo免费软件
  • 电商网站开发方案最大的推广平台
  • 腾讯云服务器学生搜索引擎优化关键词的处理
  • 如何改变网站的排版湖南疫情最新消息今天
  • 网站模板代理电话个人网站网页首页
  • 创业平台加盟seo赚钱培训
  • 网站怎么才有alexa排名推广公司品牌
  • 做企业宣传网站站长统计 站长统计
  • 王烨燃中国中医科学院seo网站推广专员
  • 广州网站建设十年乐云seo电子商务网站建设与维护
  • 基于ASP.NET的购物网站建设南宁网站公司
  • 广州网站建设 易点免费b站在线观看人数在哪
  • 怎么推广广告百度优化排名软件
  • 微网站建设公司免费优化网站
  • 做任务赚钱的网站seo牛人
  • 小程序要先做网站seo网络排名优化哪家好
  • 食品行业做网站域名查询seo