2
votes

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">
3
Is there any placeholder attribute that you are missing in your html? - nitin chawda
Hi Nitin, it is the full html tag for that input. - thangtran3112

3 Answers

2
votes

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").

2
votes

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");
0
votes

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);