3

The following xpath selects siblings of selected context. But I'm confused about the following xpath expression:

xpath://h2[contains(text(),'A few of our most popular courses')]/parent::div//div[//a[text()='SELENIUM']]/following-sibling::div[@class='rt-grid-2 rt-omega']

Question: Why this expression is boxed inside div?

Expression from above: parent::div//*div[//a[text()='SELENIUM']]*

a[text()='SELENIUM'] boxed inside div. If I'm omitting the box and re-write as follows, it's not identifying the objects.

Full code of the above can be viewed on Guru99 site.

  • 1
    doesn't feel like this question helps others in other situations. feels hard to get a general sense that browsing people would find useful to them – Michael Durrant Jan 6 at 11:39
0

the XPath expression seems to be incorrect as the part

..//div[//a[text()='SELENIUM']]

looks like does not fulfill the regular xpath pattern. If this works, it is a nice XPath hack. If I would like to select neighbors of the "SELENIUM" element on the page http://demo.guru99.com/selenium/guru99home/ , I would use the following XPath:

//h2[contains(.,"A few of our most popular courses")/parent::div[@class="rt-grid-12 rt-alpha rt-omega"]//div[@class='rt-grid-2 rt-omega']

but this will select all descendants of the grid, including the "SELENIUM" element ... and this is not exactly what was required in the example. But it is more likely what you will really need.

So looks like the formula in the example is selecting the div, which has a descendant with text "SELENIUM" and from that, all the following siblings due to // before the div and through so-called "XPath recursion" with [//a[text()='SELENIUM']]. I personally did not see such use of XPath before and I hope I would not need it in the future. But at least it is good to know, that it could work that way.

0
//div[//a[text()='SELENIUM']]

is a normal xpath pattern that sometimes the most effective way to query the proper set of nodes. What it is doing is actually takes div that has somewhere under it the links with text SELENIUM. Then you can proceed with your path from that found div element. This is the simple alternative for locating the element at the bottom of a tree and traversing to the top taking the parents.

-1

This is basically following child node under the div where text is SELENIUM this a relative x path expression and here div whoes 'a' tag is taken as a starting for locating an element where text is 'SELENIUM'

Not the answer you're looking for? Browse other questions tagged or ask your own question.