Definitely ...">

How to find the first link on a page containing this text?

If I have two links:

<div class="abc">
  <a id="def1" href="/definitely">Definitely 1</a>
  <a id="def2" href="/definitely">Definitely 2</a>    
</div>

And I want to identify the first ( def1), I thought this would work:

var linkXPath = "//div[@class='abc']//a[contains(@href,'def')][1]";

But that is not like that.

What am I doing wrong?

+3
source share
2 answers

Frequently Asked Questions Why

//someName[1]

does not select the first item //someName.

Looking at the definition of abbreviations , you can see that in fact //

//someName[1]

is equivalent to :

/descendant-or-self::node()/someName[1]

and this selects every element someNamethat is the first child of the someNameparent node.

, someName, someName , .

//someName[1]

(//someName)[1]

, :

(//div[@class='abc']//a[contains(@href,'def')]) [1]

, node, XML- . - FAQ XPath. , " " SO .

+3

XPath a ( href) div ( ), . div, a, , - a div a > div.

, :

(//div[@class='abc']//a[contains(@href,'def')])[1]

, ( ).

+2

Source: https://habr.com/ru/post/1782184/


All Articles