使用绝对XPath来定位元素是一个好习惯吗?如果不是,还有其他有效的方法来定位元素吗? [英] Is using absolute XPath to locate elements a good practice? If no, is there any other efficient way to locate elements?

查看:102
本文介绍了使用绝对XPath来定位元素是一个好习惯吗?如果不是,还有其他有效的方法来定位元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Webdriver和Java进行自动化。对于定位元素,我使用XPath。这是一个好习惯吗?

I am working on Automation using Selenium Webdriver with Java. For locating elements i use XPath. Is it a good practice?

推荐答案

绝对XPath

绝对XPath以根节点或正斜杠(/)开头。
使用绝对值的优点是,它可以非常快速地识别元素。
这里的缺点是,如果出现任何问题或者其间添加了其他标签,那么此路径将不再有效。

Absolute XPath starts with the root node or a forward slash (/). The advantage of using absolute is, it identifies the element very fast. Disadvantage here is, if any thing goes wrong or some other tag added in between, then this path will no longer works.

示例:
如果路径我们定义为

Example: If the Path we defined as


html / head / body / table / tbody / tr / th

html/head/body/table/tbody/tr/th

相对Xpath

相对xpath是路径从节点开始的路径您的选择 - 它不需要从根节点开始。

A relative xpath is one where the path starts from the node of your choise - it doesn't need to start from the root node.

它以Double forward slash(//)开头

It starts with Double forward slash(//)

语法:


// table / tbody / tr / th

//table/tbody/tr/th

使用相对xpath的优点是,你不需要提及长xpath,你可以从中间或中间开始。

Advantage of using relative xpath is, you don't need to mention the long xpath, you can start from the middle or in between.

这里的缺点是,识别元素需要更多时间,因为我们指定的是部分路径(精确路径)。

Disadvantage here is, it will take more time in identifying the element as we specify the partial path not (exact path).

这篇关于使用绝对XPath来定位元素是一个好习惯吗?如果不是,还有其他有效的方法来定位元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆