Parent and ancestors
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]
String values and 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.
See also