3

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.

Andersson
  • 51,635
  • 17
  • 77
  • 129
user3356141
  • 491
  • 1
  • 14
  • 26
  • 1
    I often use "contains", but there are more. Here are some examples: multiple condition: //h1[@id='Label' and contains(text(), 'text')] partial match: //span[contains(text(), 'Assign Rate')] starts-with: //input[starts-with(@id,'reportcombo'); value has spaces: //div[./div/div[normalize-space(.)='More Actions...']] sibling: //td[.='LoadType']/following-sibling::td[1]/select" more complex: //td[contains(normalize-space(@class), 'actualcell sajcell-row-lines saj-special x-grid-row-collapsed')] – anshul Gupta Aug 09 '18 at 07:37
  • @Andersson If you dup hammer questions based on the above granularity level, each question is duplicate of some existing at least with `selenium` tag. Can we reopen this question? – undetected Selenium Aug 09 '18 at 08:23
  • @DebanjanB , This question does not benefit the community: if it's not a duplicate, then it should be closed as "Simple typographical error": note that OP uses closing parenthesis/square brackets incorrectly. If that typo fixed then... OP just should check [how to use multiple predicates](https://stackoverflow.com/questions/568713/xpath-expression-with-multiple-predicates) – Andersson Aug 09 '18 at 08:38
  • @Andersson For the time being let us keep apart the advanced topic of _multiple predicates_ theory. OP is a new user. Our efforts should be to provide the new users a effective experiance. As you correctly pointed out `Simple typographical error`, that where new users expect help. Perhaps dup hammer was harsh here. – undetected Selenium Aug 09 '18 at 08:42
  • @DebanjanB , actually, first help is the traceback of syntax error that OP got and Andrei Suvorkov has already provided with correct answer. OK, if you have something to add - reopened – Andersson Aug 09 '18 at 09:00

1 Answers1

6

Replace:

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

with:

//h1[@id='label' and contains(text(),'text')] 
Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
  • 1
    Note that OPs' XPath is syntactically incorrect and you just copied one of the errors: replace `@text` with `text()` – Andersson Aug 09 '18 at 07:43