Demiteli
Demiteli

Reputation: 35

How could i choose HTML element by inner text?

For example, I've got structure like that:

<div>
  GLaDOS 
  <div>
    is a fictional artificially intelligent computer system 
  </div>
  from the video game series 
  <div>
    Portal
  </div>
</div>

<div>
  from the video game series 
  <div>
    is a fictional artificially intelligent computer system 
  </div>
  123 
  <div>
    Portal
  </div>
</div>

And I want to choose element which contains "from the video game series" between first and second inner div. How should xpath expression looks like?

Upvotes: 0

Views: 70

Answers (2)

fabrik
fabrik

Reputation: 14365

This solution is as solid as the source (e.g. if the source'll change, it'll be broken):

//text()[contains(., 'from the video game series')][preceding-sibling::div and following-sibling::div]

You can test it here.

Upvotes: 0

Markus
Markus

Reputation: 3357

To select the div element which contains the text from the video game series, you can use the XPath expression //div[contains(., 'from the video game series')]".

To limit that selection to text between the first and second div child, try the following: //div[contains(text()[count(preceding-sibling::div) = 1 and count(following-sibling::div) > 0], 'from the video game series')].

Upvotes: 1

Related Questions