How to extract the ID attribute of a DOM element, if i know corresponding text through Selenium Webdriver and Java

deepak mishra :

I used following code:

driver.getPageSource().contains("My value in text box");

This will let me know, element present in dom or not.

Now, i need to know , value containing text-box "My value in text box". What is id of this text box.

DebanjanB :

To extract the id of a dom element you don't need to invoke getPageSource(). You can simply use the getAttribute() method as follows:

String elementID = driver.findElement(By.xpath("//*[contains(text(),'My value in text box')]")).getAttribute("id");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97444&siteId=1