简体   繁体   English

如何在 xpath 元素上定位准确的文本?

[英]How to locate exact text on xpath element?

I am trying to locate the xpath that matches the exact text "Manufacturer" but it is not working.我试图找到与确切文本“制造商”匹配的 xpath,但它不起作用。

I cannot use "contains" as there is another xpath element that has text "Discontinued by Manufacturer" in it.我不能使用“contains”,因为还有另一个 xpath 元素,其中包含“Discontinued by Manufacturer”文本。 I need a solution where I can use the same thing on other urls which sometimes doesn't have other elements containing the text "Manufacturer" like "Discontinued by Manufacturer" and has only just the "Manufacturer"我需要一个解决方案,我可以在其他 url 上使用相同的东西,有时没有其他包含文本“制造商”的元素,如“制造商停产”,只有“制造商”

The url is https://www.amazon.com/BEAKEY-Foundation-Blending-Flawless-Multi-colored/dp/B01F36JEXE/ref=sr_1_22?dchild=1&keywords=cosmetics&qid=1625014752&sr=8-22 if anyone needs a reference网址是https://www.amazon.com/BEAKEY-Foundation-Blending-Flawless-Multi-colored/dp/B01F36JEXE/ref=sr_1_22?dchild=1&keywords=cosmetics&qid=1625014752&sr=8-22如果有人需要参考

here is what I'm seeing when I try to search for "Manufacturer" image of error这是我尝试搜索“制造商”错误图像时所看到的

There are 4 Manufacturer on that page.该页面上有 4 个制造商。

you can try this xpath :你可以试试这个 xpath :

(//ul[contains(@class, 'a-unordered-list')]/descendant::span[@class='a-text-bold'])[1]

to match匹配

Is Discontinued By Manufacturer ‏ : ‎ No

and

(//ul[contains(@class, 'a-unordered-list')]/descendant::span[@class='a-text-bold'])[5]

to match :匹配 :

Manufacturer ‏ : ‎ BEAKEY

由于有 2 个带有文本“制造商”的 span 元素,因此应该适合:

"(//span[contains(text(),'Manufacturer')])[last()]"

If you want to match the content of a text node or element exactly, use the "=" operator, not contains() .如果要完全匹配文本节点或元素的内容,请使用“=”运算符,而不是contains()

It's a common XPath mistake to use contains() in place of "=".使用contains()代替“=”是一个常见的 XPath 错误。 In fact, it's a common mistake to guess what a function does from its name, rather than reading the spec.事实上,根据函数的名称猜测函数的作用,而不是阅读规范,这是一个常见的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM