1

I have written this expression //*[contains(text(), "Brand:" )] for the below HTML code.

<div class="info-product mt-3">
  <h3>Informazioni prodotto</h3>


  Brand: <span class="brand_title font-weight-bold text-uppercase"><a href="https://mammapack.com/brand/ava">Ava</a></span><br> SKU: 8002910009960<br> Peso Lordo: 0.471 kg <br> Dimensioni: 44.00 × 145.00 × 153.00 mm<br>

  <p class="mt-2">
    AVA BUCATO A MANO E2 GR.380</p>
</div>

The xpath that I have written is not working I want to select Node that contains text Brand:. Can someone tell me my mistake?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
stack
  • 39
  • 1
  • 6

1 Answers1

3

Your XPath,

//*[contains(text(), "Brand:")]

in XPath 1.0 will select all elements whose first text node child contains a "Brand:" substring. In XPath 2.0 it is an error to call contains() with a sequence of more than one item as the first argument.

This XPath,

//*[text()[contains(., "Brand:")]]

will select all elements with a text node child whose string value contains a "Brand:" substring.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks for the update! This is what I wanted to ask you about. Why can't I upvote here twice? – Prophet Feb 24 '22 at 15:12
  • @Prophet, thanks. The old Q/A on this topic also didn't adequately address the XPath 1.0 vs 2.0+ difference, so I've [added a very detailed answer there](https://stackoverflow.com/a/71255563/290085) too to remedy that. – kjhughes Feb 24 '22 at 17:02