2

I wonder about how I can use selenium webdriver to find the default text of an element ? In the browser, the input field displays a default value: 'Project 1', but I cannot get this text through the method getText() of this WebElement.

<input class="title viewData" id="sprojectName" maxlength="255" name="projectName" type="text" projectinfo="1">
2
  • Is there any placeholder attribute that you are missing in your html? Commented May 23, 2014 at 20:04
  • Hi Nitin, it is the full html tag for that input. Commented May 26, 2014 at 15:42

3 Answers 3

2

getText() returns "the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace." You need something like getAttribute("value") or getAttribute("placeholder").

Sign up to request clarification or add additional context in comments.

Comments

2

The getText() method is for retrieving a text node between element tags for example:

Eg:

<p>New</p>

But usually the value in the text box is saved to "value" attribute. So the below statement will work:

findElement(By.id("ElementID")).getAttribute("value");

Comments

0

Yes, I will try to see if getAttribute("value") work. In the meantime, I have solved the problem using JavaScript executor:

String jsStatement = "return document.getElementById('" + elementId + "')." + "value" + ";";
JavascriptExecutor js = null;
if (session instanceof JavascriptExecutor) {
    js = (JavascriptExecutor)session;
}
return (String) js.executeScript(jsStatement);  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.