简体   繁体   中英

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.

I cannot use "contains" as there is another xpath element that has text "Discontinued by Manufacturer" in it. 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"

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

here is what I'm seeing when I try to search for "Manufacturer" image of error

There are 4 Manufacturer on that page.

you can try this 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() .

It's a common XPath mistake to use contains() in place of "=". In fact, it's a common mistake to guess what a function does from its name, rather than reading the spec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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