I use this function with good results:
static WebElement getElementsWithAlteratingNames(String PartialName, String Tag, WebDriver driver, String Attribute) {
try {
List<WebElement> Elements = driver.findElements(By.tagName(Tag));
String[] ElementStrings = new String[Elements.size()];
System.out.println(Elements.size());
for (int LoopCounter = 0; LoopCounter < Elements.size(); LoopCounter++) {
ElementStrings[LoopCounter] = Elements.get(LoopCounter).getAttribute(Attribute).toString();
if (ElementStrings[LoopCounter].contains(PartialName)) {
System.out.println(ElementStrings[LoopCounter]);
return Elements.get(LoopCounter);
}
}
} catch (Exception e) {
System.out.println("Could not generate a list of elements");
}
return null;
}
I've kept it fairly abstract so you can easily costumize it to serve the individual needs of your wanted Element.
Update
Since the Method is just not good and throws away efficiency if made a small update that does pretty much the same:
static WebElement findElem(String Tag, String Att, String Value){
List<WebElement>Elems = driver.findElements(By.tagName(Tag));
for(WebElement Elem : Elems){
if(Elem.getAttribute(Att).contains(Value)) return Elem;
}
}
Since the code above isn't something that would pass a code review if one of my team did it I wouldn't feel good to leave it that way.