Reputation: 53
I'm using Java and Selenium. I know I could get visible text using the getText() method/ For example, if I supply the ID (via XPath or CSS) I could do getText() on this to get visible text. But what if I want to do the opposite. Let's say I know what the visible text is and I want to use the text to find another attribute? For example, let's say I have the following HTML markup:
<div class="" title="Card"/>
<div id="99999cardName" class="cardName editInline" title="Click to edit">ZZZ</div>
<div id="99999cardNumber" class="cardNumber">4590 6565 6565 6565</div>
In this example, suppose I knew the value 4590 6565 6565 6565
and I wanted to get the value 99999cardNumber
- how would I do it?
Thanks
Upvotes: 2
Views: 4426
Reputation: 6962
Use xpath in such a way that it contains text of that element. Here's how to do it -
String idVal = driver.findElement(By.xpath("//div[contains(text(),'4590 6565 6565 6565')]")).getAttribute("id");
System.out.println(idVal);
Hope this helps.
Upvotes: 1