[英]How a write a common XPath for same text displayed for different HTML tags?
I want to write a common XPath for the result displayed for my searched text 'Automation Server'我想为我的搜索文本“自动化服务器”显示的结果编写一个常见的 XPath
The same text is displayed for td
HTML tags as well as for div
html tags as shown below, and I wrote XPath as below based on my understanding by going through different article td
HTML 标签和div
html 标签显示相同的文本,如下所示,根据我通过不同文章的理解,我写了 XPath 如下
displayed_text = //td[contains(text(),'Automation Server') or div[contains(text(),' Automation Server ')]
<td role="cell" mat-cell="" class="mat-cell cdk-cell cdk-column-siteName mat-column-siteName ng-star-inserted">Automation Server</td>
<div class="change-list-value ng-star-inserted"> Automation Server </div>
The operator you are looking for in XPath is |
您在 XPath 中寻找的运算符是|
. . It is a union operator and will return both sets of elements.它是一个联合运算符,将返回两组元素。
The XPath you are looking for is您正在寻找的 XPath 是
//td[contains(text(),'Automation Server')] | //div[contains(text(),'Automation Server')]
This XPath,此 XPath,
//*[self::td or self::div][text()[normalize-space()='Automation Server']]
will select all td
or div
elements with an immediate text node whose normalize string value equals 'Automation Server'
.将 select 具有直接文本节点的所有td
或div
元素,其规范化字符串值等于'Automation Server'
。
|
is not logical-OR or "OR-like".不是逻辑或或“类似或”。contains(text(), "string")
only tests the first text node child. contains(text(), "string")
仅测试第一个文本节点子节点。A few alternatives to JeffC answer, using common properties for both: JeffC答案的一些替代方案,对两者都使用共同属性:
1. use the *
as a wildcard for any element: 1. 使用*
作为任何元素的通配符:
//*[contains(@class,'ng-star-inserted') and normalize-space(text())='Automation Server']
2. use in addition the local-name()
function to narrow down the names of the elements: 2. 另外使用local-name()
function 来缩小元素的名称:
//*[local-name()[.='td' or .='div']][contains(@class,'ng-star-inserted') and normalize-space(text())='Automation Server']
The normalize-space() function can be used to clean-up the optional white space, so a = operator can be used. normalize-space() function 可用于清理可选的空白,因此可以使用= 运算符。
You could use the following XPath to test the local-name()
of the element in a predicate and whether it's text()
contains the phrase:您可以使用以下 XPath 来测试谓词中元素的local-name()
以及它的text()
是否包含该短语:
//*[(local-name() = "td" or local-name() = "div") and contains(text(), "Automation Server")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.