Search code examples
javaseleniumselenium-webdriver

Using Selenium WebDriver to retrieve the value of an HTML input


In the HTML of a web application there is the following code:

<input type="text" name="prettyTime" id="prettyTime" class="ui-state-disabled prettyTime"  readonly="readonly">

A string displaying the time is actually shown on the page.

In Selenium WebDriver, I have a WebElement object referring to the <input> using:

WebElement timeStamp = waitForElement(By.id("prettyTime"));

I want to get the value of the WebElement, or, in other words, what is printed on the page. I tried all the WebElement getters and nothing has been retrieving the actual value that the user sees.


Solution

  • This is kind of hacky, but it works.

    I used JavascriptExecutor and added a div to the HTML, and changed the text in the div to $('#prettyTime').val()

    I then used Selenium to retrieve the div and grab its value. After testing the correctness of the value, I removed the div that was just created.