You can always go with xpath if there is no uniqueness with an attribute. E.g. if you want to find an element with the text foo and name button , then I will prefer xpath as below, if the name is not unique there:
//*[@name='button' and text()='foo']
Or For another class, but with the same name
//button[@name='button' and @class='xyz']
or for other text but with the same name
//input[@name='button' and contains(text(),'Click Here')]
or for different tags, but with the same name
//button[@name='button'] //input[@name='button']
Just go with any unique property and create a customized xpath.
Hope you can also use java script for this, e.g. for
WebElement butttonToClick = driver.findElement(By.name("button")); ((JavascriptExecutor)driver).executeScript("arguments[1].click();",butttonToClick );
Where arguments[1] means the second element with the same name.
source share