11

I'm working with a DOM node:

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

How can I get its value? I'm struggling since element.getText() does not work and returns a blank.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

10 Answers10

21

Try this:

WebElement element = driver.findElement(By.id("id value"));  
String val = element.getAttribute("innerText")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eknath
  • 624
  • 4
  • 11
2

I presume the element in question is an <input> element, so you may be able to use the element.getAttribute(String attribute) method like so:

String value = element.getAttribute("value");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom Trumper
  • 472
  • 2
  • 8
1

This input tag is disabled, hence element.getText() returns a blank value.

Use element.getAttribute("textContent") instead.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Harsh
  • 65
  • 1
  • 9
1

You may be looking for the placeholder of an input text, because you might try:

element.getAttribute("placeholder");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

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 previous comments:

element_locator.get_attribute('InnerText')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stipe
  • 480
  • 5
  • 18
  • In Firefox (at least 106.0.2), it is "Inspector" (invoke it by menu *Tools* → *Browser Tools* → *Web Developer Tools*. Or if the *"Tools"* menu is hidden: `Alt` + `T`. Or keyboard shortcut `Ctrl` + `Shift` + `I` to open Web Developer Tools): 1) Click *"Pick an element from the page"* (or `Shift` + `Ctrl` + `C`). 2) Left click on an element on the page. 3) Right-click on the selected HTML line. 4) Choose *Copy* → *XPath*. – Peter Mortensen Nov 24 '22 at 00:23
0

I had the exact same issue! This post solved it for me: How can I get the current contents of an element in webdriver

I used:

element = driver.find_elements_by_xpath(
            '//button[@class="size-grid-dropdown size-grid-button"]')
element.text
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
geppy
  • 101
  • 1
  • 3
  • why are you posting a python answer to an obviously *java* question? Even if you say it's not clear at all the rest of the answers are written for java... – gabriel garcia Jun 21 '20 at 12:47
0

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 attributes. In your case: type, data-bind, EnterpriseId and autocomplete. No value will be as this attribute isn’t created.

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

String example = driver.findElement(ByLocator(("")).getAttribute("any attribute of your input");
System.out.println(example);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

As other's suggested, HTML's input nodes don'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 the webdriver's API for a method that's able to retrieve the value attribute.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gabriel garcia
  • 368
  • 3
  • 17
0

As a bonus to evaluate innerText of an element within Selenium:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yourEl")));
wait.until(ExpectedConditions.attributeToBe(By.id("yourEl"), "innerText", yourValue));

Documentation: attributeToBe

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave Yu
  • 315
  • 4
  • 15
0

Faced a similar issue, I wanted web driver to wait until a particular text appeared in 2 web elements.so did a do while until the condition passed.

String val = "", val1 = "";
        do {
            val = (commonMethods.getElementByXpath(chromedriver, 1000, elements.trasmitBandwidthText))
                    **.getAttribute("innerText");**

            val1 = (commonMethods.getElementByXpath(chromedriver, 1000, elements.receiveBandwidthText))
                    **.getAttribute("innerText");**

            System.out.println(pod + "--- " + val + " value !!!!!!" + val1);
        } while (!(val.contains(pod) && val1.contains(pod)));

.getAttribute("innerText") helps get text inside an element.

merita
  • 1
  • 1