Reputation: 4062
I have a html page with some nested div tags. The tags do have an ID but it is not a fixed value. It changes as users add and delete items on the page. The best way to locate the element I am looking for is by text.
Each parent has the Text "Clean" - Cannot locate from here as it is not unique.
Each parent has a child, the end part of the text in the child is different.
E.g. The text is "Clean feed crm" or "Clean feed escr" and so on
E.g. Parent "Clean" -> child "Clean feed crm" Parent "Clean" -> child "Clean feed escr"
I would like to find the Parent tag the text "Clean" from the child "Clean feed escr" From the html sample below it's current ID is id="operations_edit_process_list_task_4 But this ID will change so i cannot use the ID to get to this element.
I can locate the child "Clean feed escr" using XPATH. I do not know how to go up the tags to get the "Clean" text
My XPATH is:
//span[contains(text(), "Clean feed escr")]
The HTML sample is (I have removed some div tags, it will be too long to paste otherwise)
<div id="operations_edit_process_list_task_3">
<span>
<span class="myinlineblock" title="Clean" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;width:100%;">Clean</span>
</span>
<span/>
<span>
</div>
// Few More div tags
<div id="operations_edit_process_list_task_3_1">
<span>
<span class="myinlineblock" title="Clean feed escr" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;width:100%;margin-right:-14px;">Clean feed escr</span>
</span>
<span>
</div>
// Few More div tags
<div id="operations_edit_process_list_task_4">
<span>
<span class="myinlineblock" title="Clean" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;width:100%;">Clean</span>
</span>
<span/>
<span>
</div>
// Few More div tags
<div id="operations_edit_process_list_task_4_1">
<span>
<span class="myinlineblock" title="Clean feed orchard" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;width:100%;margin-right:-14px;">Clean feed orchard</span>
</span>
</div>
I think ancestor could be used t get to it's immediate parent tag but i do not know it's syntax. I have tried the following:
//span[contains(text(), "Clean feed escr")]//ancestor::div[contains(text(), "Clean")]
Thanks, Riaz
Upvotes: 1
Views: 1692
Reputation: 12613
This in my opinion this would be a much cleaner approach:
"//div[.//span/text()='Clean feed escr']/following::div[.//span/text()='Clean'][1]"
Upvotes: 1
Reputation: 371
This video will surely help you out as it has properly defined all methods of XPath like parent, following-sibling, preceding sibling and may other.
Upvotes: 0
Reputation: 4062
With the help of the suggestions below I have managed to locate the element using the Preceding axis. Preceding because I want to go up the tree to it's parent. Not the 'following' axis. following goes down the tree to it's children. The Xpath is:
//div[.//span/text()='Clean feed escr']/ancestor::div[1]/preceding::div[1]//span[@title="Clean"]
Upvotes: 1
Reputation: 42518
To get the next div sibling by filtering the span children:
"//span[@title='Clean feed escr']/ancestor::div[1]/following-sibling::div[.//span[@title='Clean']][1]"
Upvotes: 1