How to get inner text of an element in Selenium?

Viewed 78678

I'm working with a DOM node

<input type = "form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">

How can I get it's value? I'm struggling since element.getText() does not work and return blank.

Any suggestions would be of great help.

9 Answers

Are you looking for the placeholder of an input text? 'cause you might try

element.getAttribute("placeholder");

You can go to your browser -> open developer tools -> inspect element you want to take attribute from -> click Properties-> check if that value is in InnerText

Then do as it is mentioned in above comments:

element_locator.get_attribute('InnerText')

It is a bit late but it may help for other ones who are looking for a similar solution: It works definitely, as I've tested it several times:

<input type = "form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">

In your example, you don´t have any innerText. So you can only get attributes as mentioned before with the existing atributes. In your case: type, data-bind, EnterpriseId and autocomplete. No value will be as this attribute isn´t created.

If you want to get ony existing, this should be fine:

String example= driver.findElement(ByLocator(("")).getAttribute("any attribute of your input"); 
System.out.println(example);

As other's suggested, HTML's input nodes doesn't have a text attribute because they can store data in multiple formats in a value attribute. This can be easily seen in the HTML input API specification where this form control can be of type radio, date, file upload and many more.

So, in your specific case, I'd suggest you check webdriver's API for a method that's able to retrieve the value attribute.

Related