Reputation: 2905
Hi i want to get value of html element using web driver how can i get it?I am explaining the scenario as below. I have a span element as below with value between the starting and closing tag .How can i get it?
<span id="foo">
some value
</span>
Upvotes: 1
Views: 1322
Reputation: 3278
Try locating the element using XPath instead of ID, then using either
driver.findElement(By.xpath(“xpath for your lbl“)).getText()
or
String st = driver.findElement(By.xpath(“xpath to your lbl“)).getAttribute(“value”);
Source : SeleniumWiki
Upvotes: 0
Reputation: 11581
You have to use the webElement.getText()
for that.
I worte a small unit test for you:
public class TestGetText
{
@Test
public void shouldReadSomevalue()
{
final WebDriver webDriver = new HtmlUnitDriver();
webDriver.get("http://s2server.de/stackoverflow/11719445.html");
final WebElement webElement = webDriver.findElement(By.id("foo"));
final String text = webElement.getText();
assertEquals("some value", text);
}
}
Upvotes: 3
Reputation: 2002
Try below solution -
String test = driver.findElement(By.id("lbHome")).getText();
System.out.println(test);
Upvotes: 1