kroe761
kroe761

Reputation: 3534

XPath to find dynamic parent/ancestor element from current element?

I am writing selenium tests for a dynamic material angular web app. I can successfully locate the element I want, but then I need to go back up the DOM to the nearest <mat-form-field> tag. I know how to navigate upwards using xpath (By.XPath("../..")) but I don't always know how many elements there will be until I get to the <mat-form-field> tag. How could I locate that element dynamically?

Upvotes: 2

Views: 1473

Answers (1)

kjhughes
kjhughes

Reputation: 111541

This XPath,

//target[@id="i1"]/ancestor::mat-form-field[1]

will select the closest ancestor mat-form-field element from the targeted target element.

Upvotes: 4

Related Questions