How to get value from <h3> tag in Selenium WebDriver, Java
4 answers
It is written in C #, but converting it to Java is not difficult:
/** Declare variables **/
string descriptionTextXPath = "//div[contains(@class, 'description')]/h3";
/** Find the element **/
IWebElement h3Element = driver.FindElement(By.XPath(descriptionTextXPath));
/** Grab the text **/
string descriptionText = h3Element.Text;
Depending on whether you have other div elements with the class 'description', you may need to further fine-tune your results.
Just in case:
To find all the divs on the page that act as descriptions for future reference, you can do this:
/** Declare XPath variable **/
string descriptionElementXPath = "//div[contains(@class, 'description')]";
/** Grab all description div elements **/
List<IWebElement> descriptionElements = driver.FindElements(By.XPath(descriptionElementXPath ));
You can then use forloop to traverse each element and capture the data you need.
# Java:
/** **/
String descriptionTextXPath = "//div[contains(@class, 'description')]/h3";
/** **/
IWebElement h3Element = driver.findElement(By.xpath(descriptionTextXPath));
/** **/
String descriptionText = h3Element.toString();
String descriptionText = h3Element.getText();
+3