Samsung Galaxy SI...">

How to get value from <h3> tag in Selenium WebDriver, Java

I have html code:

<div class="description">
<h3><strong>Samsung</strong> Galaxy SII (I9100)</h3>
<div class="phone_color"> ... </div>
...
</div>

I want to get the Samsung Galaxy SII (I9100) tag value from / h 3> using Selenium 2 (WebDriver)

Does anyone know how to do this?

+5
source share
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
WebElement divElement = driver.findelement(By.classname("description"));
String str = divElement.getText();
System.out.println(str);

.. .. :)

+2
WebElement divElement = driver.findelement(By.classname("description"));
String str = divElement.getText();
enter code here

str contains a value.

0
source
webDriver.findElement(By.tagName("h3"));
0
source

All Articles