Reputation: 36267
I have the HTML in the screenshot, I can get the node with text in it using:
//div[contains(., 'Property Land')]
but now I want to go up one level and then find the table child of the first ancestor.
I tried:
//div[contains(., 'Property Land')]::div.panel
but this is failing. How to I get immediate ancestor of the text containing node?
Upvotes: 3
Views: 2301
Reputation: 111591
First, let's address the question implied by your title and assumed solution.
The parent abbreviation, ..
, can find an element's parent.
The ancestor::
axis can be used to select among an element's ancestors.
The first ancestor with a specific class of //div[contains(., 'Property Land')]
can be selected by
//div[contains(., 'Property Land')]/ancestor::div[@class="panel panel-primary"][1]
contains()
Second, realize that your XPath,
//div[contains(., 'Property Land')]
actually already selects not only the div
that immediately contains the substring, "Property Land"
, but also all ancestor div
elements because their string values necessarily also contain the substring, "Property Land"
.
Therefore, you only need add a predicate to differentiate among all the div
that contain the "Property Land"
substring. You say you'd like the one with a panel
class. Here again, be careful to realize that contains()
tests for substring – both "panel panel-primary"
and "panel-heading"
contain the "panel"
substring. Let's suppose that it's "panel"
specifically that you want.
Then, use this XPath:
//div[contains(., 'Property Land')]
[contains(concat(' ',@class,' '), ' panel ')]
to avoid matching other div
elements with class
attribute values that contain other "panel"
substring.
Upvotes: 3