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 value Samsung Galaxy SII (I9100) from /h3> tag using Selenium 2 (WebDriver)
Any one know how to do it?
This is written in C#, but it shouldn't be difficult to convert it over to Java:
/** 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 'description' class, you may need to further fine tune your results.
Just in case:
In order to find all divs on the page that act as descriptions for further use, 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 a forloop to go through each element and grab the data you need.
The following is the conversion of above C# code to Java:
/** 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.toString();
OR,
String descriptionText = h3Element.getText();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With