Locating Techniques 定位技术
Useful XPATH patterns 有效的XPATH模式
text 文本节点
Not yet written - locate elements based on the text content of the node.
还没有成文 - 基于节点的文本内容定位元素。
starts-with
Many sites use dynamic values for element’s id attributes, which can make them difficult to locate. One simple solution is to use XPath functions and base the location on what you do know about the element. For example, if your dynamic ids have the format <input id="text-12345" /> where 12345 is a dynamic number you could use the following XPath: //input[starts-with(@id, 'text-')]
许多站点,id属性值使用动态值,这将使定位变得困难。一个简单的解决方案是,使用XPath函数和基于你知道的元素定位其他元素。例如,如果您的动态ids有 <input id="text-12345" />的格式,12345是一个动态数字,你可以使用如下XPath: //input[starts-with(@id, 'text-')]
contains 包含,孩子节点
If an element can be located by a value that could be surrounded by other text, the contains function can be used. To demonstrate, the element <span class="top heading bold"> can be located based on the ‘heading’ class without having to couple it with the ‘top’ and ‘bold’ classes using the following XPath: //span[contains(@class, 'heading')]. Incidentally, this would be much neater (and probably faster) using the CSS locator strategy css=span.heading
如果一个元素能按值定位,此值可能是周围环绕的其他文本,contains函数就可以运用。例如, <span class="top heading bold">可以基于‘heading’class定位,无需处理‘top’ and ‘bold’classes,使用如下XPath: //span[contains(@class, 'heading')]。顺便提一下,这将比使用CSS定位策略更简洁(且可能更快)。
css=span.heading
siblings 兄弟节点
Not yet written - locate elements based on their siblings. Useful for forms and tables.
还没有成文 - 基于兄弟节点定位元素。对于表单和表格很有用。
Starting to use CSS instead of XPATH 开始使用CSS替代XPATH
Locating elements based on class 基于类定位元素
In order to locate an element based on associated class in XPath you must consider that the element could have multiple classes and defined in any order, however with CSS locators this is much simpler (and faster).
- XPath: //div[contains(@class, 'article-heading')]
- CSS: css=div.article-heading
[个人学习记录]

浙公网安备 33010602011771号