0
<div class="plan"> don't get <a class="ensure" href="https://google.com">linktext</a> more sentence here </div>

I'm trying to get Xpath to assert the whole sentence which is all the text with apostrophe and (link + its text) in 1 expression as below. But its not working, please help?

//div[contains(text() = 'don\'t get more sentence here']/a[contains(text()='linktext')]/@href']
Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
LisaA
  • 31
  • 2
  • 7

2 Answers2

1

The way you specify an apostrophe without conflicting with single quote delimiters will vary per the language hosting the XPath.

You likely can skirt the issue here, however, if, as I suspect, you're over specifying the targeted div. If matching on the link anchor text and the end of the div suffice, you can use this XPath:

//div[a[. = 'linktext'] and ends-with(., ' more sentence here ')]
Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
1

It's not entirely clear what you're trying to accomplish with all of this, but here is one expression that should work:

//div[text()[contains(., "don't get")] and 
      text()[contains(., "more sentence here")]
     ]/a[contains(text(), 'linktext')]/@href

This should also work:

//div[contains(., "don't get") and 
      contains(., "more sentence here")
     ]/a[contains(., 'linktext')]/@href

The div in your example has two separate text nodes, so you can't use a single contains() on both of them at the same time.

JLRishe
  • 99,490
  • 19
  • 131
  • 169