Deepak
Deepak

Reputation: 2905

How can we pick html elements value using web driver

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

Answers (3)

TheLifeOfSteve
TheLifeOfSteve

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

d0x
d0x

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

Rohit Ware
Rohit Ware

Reputation: 2002

Try below solution -

String test = driver.findElement(By.id("lbHome")).getText();
System.out.println(test);

Upvotes: 1

Related Questions