Using getText in Selenium 2 Webdriver

In this example I will tell you how you can use selenium command getText in Selenium 2 Webdriver. I had to use this as selenium.getText() was not working on Webdriver when I was using HtmlUnitDriver. It was working when I was using FirefoxDriver. I had to convert the selenium.getText() to make it work on both HtmlUnitDriver and FirefoxDriver.

Here is the HTML code I that I want to check:


The code for getText in Selenium RC is as under:
This will return Selenium Wiki

The same code for getText in HtmlUnitDriver and FirefoxDriver is as under:
This will return Selenium Wiki

If you found this solution helpful or have something extra to add, feel free to share it here by commenting below.

4 thoughts on “Using getText in Selenium 2 Webdriver

  1. Krishnam says:

    Hey. nice article. However, getText() does not return the value of the field. The getValue of seleniumBackedWebDriver works although. not sure why.

    // The below code does not return the value of TextArea
    element = webDriver.findElement(By.xpath(RestAPI.TXTAREA_RESPONSE));
    String value = element.getText();

    // This statement works without any surprises.
    getNodeResponse = seleniumBackedWebDriver.getValue(RestAPI.TXTAREA_RESPONSE);

  2. Deepak says:

    Hi, i can not switch in iframe because of there is not any name or id of the iframe, so how can i switch in that iframe. and after switching into that frame i have to enter text.
    this problem only rise in HtmlUnitDriver. other browser work nice.

  3. Ram says:

    String st = driver.findElement(By.xpath(“//*[@id=’ctl00_mainContentPlaceHolder_txtSecurityCode’]”)).getAttribute(“value”);


    This will work.

Leave a Reply

Your email address will not be published. Required fields are marked *