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

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


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");