jamesfzhang
jamesfzhang

Reputation: 4473

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.

Upvotes: 154

Views: 324516

Answers (11)

Jorge Luis Bravo
Jorge Luis Bravo

Reputation: 1

It works for me in java code:

@FindBy(how = How.XPATH, using = "//input[@formcontrolname='name']")
private WebElement name;

try {
      JavascriptExecutor jse;
      String value = (String)jse.executeScript("return arguments[0].value", name);
      return value;
    } catch(Exception ex) {
      return "";
    }

Upvotes: 0

Sajid Manzoor
Sajid Manzoor

Reputation: 517

For Python bindings it will be:

element.get_attribute('value')

Upvotes: 22

Vedika Sadavarte
Vedika Sadavarte

Reputation: 9

Java users:

To get what is printed on the page, we need to use the getText() method.

getText() method

The getText() method returns the visible inner text of a web element.

getAttribute() method

On the other hand, the getAttribute() method fetches the value of the attribute we wish to retrieve.

Example:

<input name="Title" type="text" value="LambdaTest" /> Welcome to LambdaTest </input>

getText()

driver.findElement(By.name("Title")).getText();

Output of above code => Welcome to LambdaTest

getAttribute():

  1. driver.findElement(By.name("Title")).getAttribute("value");

    Output of above code => LambdaTest

  2. driver.findElement(By.name("Title")).getAttribute("type");

    Output of above code => text

Source: Difference between getText() And getAttribute() in Selenium WebDriver

Upvotes: 0

Rajesh
Rajesh

Reputation: 47

Use

element.GetAttribute("value");

Even though if you don't see the "value" attribute in the HTML DOM, you will get the field value displayed in the GUI.

Upvotes: 4

Vfleitao
Vfleitao

Reputation: 58

Following ragzzy's answer, I use

public static string Value(this IWebElement element,
                           IJavaScriptExecutor javaScriptExecutor)
{
    try
    {
        string value = javaScriptExecutor.ExecuteScript("return arguments[0].value", element) as string;
        return value;
    }
    catch (Exception)
    {
        return null;
    }
}

It works quite well and does not alter the DOM.

Upvotes: 5

Praveen
Praveen

Reputation: 387

You can do it like this:

webelement time = driver.findElement(By.id("input_name")).getAttribute("value");

This will give you the time displaying on the webpage.

Upvotes: 31

raggzy
raggzy

Reputation: 81

As was mentioned before, you could do something like this:

public String getVal(WebElement webElement) {
    JavascriptExecutor e = (JavascriptExecutor) driver;
    return (String) e.executeScript(String.format("return $('#%s').val();", webElement.getAttribute("id")));
}

But as you can see, your element must have an id attribute, and also, jQuery on your page.

Upvotes: 5

e1che
e1che

Reputation: 1251

With Selenium 2, I usually write it like this:

WebElement element = driver.findElement(By.id("input_name"));
String elementval = element.getAttribute("value");

Or

String elementval = driver.findElement(By.id("input_name")).getAttribute("value");

Upvotes: 18

jamesfzhang
jamesfzhang

Reputation: 4473

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.

Upvotes: -3

prestomanifesto
prestomanifesto

Reputation: 12796

Try element.getAttribute("value")

The text property is for text within the tags of an element. For input elements, the displayed text is not wrapped by the <input> tag, instead it's inside the value attribute.

Note: Case matters. If you specify "Value", you'll get a 'null' value back. This is true for C# at least.

Upvotes: 254

saille
saille

Reputation: 9181

If the input value gets populated by a script that has some latency involved (e.g. AJAX call) then you need to wait until the input has been populated. E.g.

var w = new WebDriverWait(WebBrowser, TimeSpan.FromSeconds(10));
            w.Until((d) => {
                // Wait until the input has a value...

                var elements = d.FindElements(By.Name(name));

                var ele = elements.SingleOrDefault();

                if (ele != null)
                {
                    // Found a single element

                    if (ele.GetAttribute("value") != "")
                    {
                        // We have a value now
                        return true;
                    }
                }

                return false;
                });

        var e = WebBrowser.Current.FindElement(By.Name(name));

        if (e.GetAttribute("value") != value)
        {
            Assert.Fail("Result contains a field named '{0}', but its value is '{1}', not '{2}' as expected", name, e.GetAttribute("value"), value);
        }

Upvotes: 1

Related Questions