Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath expression with both an ID and contains Text

So I have the following tag I want to find using Xpath:

 <h1 id="label">This is the text</h1>

Normally, I would write it like this:

//h1[@id='Label' and text() = "This is the text"]

Or, if I want to extract just partion of the text, I use the following:

//h1[text()[contains(.,'text')]]

Now, I tried to look for tutorials wether you can mix both statements: so I want to have an Xpath Expression that contains both @id and contains the text "Text". Is such a thing even possible?

I tried doing this:

//h1[@id='Label' and [contains(@text,"text"])] 

But that doesn't give me a propper result.

like image 225
user3356141 Avatar asked Sep 18 '25 16:09

user3356141


1 Answers

Replace:

//h1[@id='Label' and [contains(@text,"text"])] 

with:

//h1[@id='label' and contains(text(),'text')] 
like image 101
Andrei Suvorkov Avatar answered Sep 20 '25 06:09

Andrei Suvorkov