when there are multiple matches for the xpath , how to identify the desired locator uniquely

--

when there are multiple matches for the xpath , how to identify the desired locator uniquely.

For instance, //a[@title=’Article Title’] gives 20 matches

So to identify it uniquely , you can put it in brackets and mention the desired locator’s number (Here it’s 6) , and then search so only one result.

Example locators-

(//a[@title='Article Title'])[6]

(//img[@alt='User Avatar'])[1]

Example code with Playwright Java

  Locator myImage = page.locator("(//img[@alt='User Avatar'])[1]");
myImage.hover();

--

--

No responses yet