How can I get the inner text of an element in Selenium?
Asked Answered
U

10

13

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.

Urissa answered 12/5, 2017 at 9:11 Comment(5)
Can you show us some of your research please?Ossify
have you tried element.getAttribute("value") Orangewood
Possible duplicate: How to gettext() of an element in Selenium WebdriverHeartache
Reference for the disabled attribute. But what is data-bind? What is the consequence for the doubled type attribute?Heartache
Perhaps the title should be more specific?Heartache
C
22

Try this:

WebElement element = driver.findElement(By.id("id value"));  
String val = element.getAttribute("innerText")
Cairn answered 12/5, 2017 at 9:29 Comment(5)
can you share your By locator ? and code that you triedCairn
dude, u r deviating from the topic, why do u need the By locator here. I am just asking how to get an input type="text" value when the value attribute is not present for this element in the DOMUrissa
might be your locator and the way u wrote ur code snippet have some error.Cairn
I'm getting: AttributeError: 'WebElement' object has no attribute 'getAttribute' but this is in pythonRelic
that's because you're using webdriver's java API instead of python's APIFidole
T
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");
Thereafter answered 12/5, 2017 at 9:13 Comment(3)
there is no value attribute as I can see in this element tagUrissa
Ok for the given element <input type = "form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">, what exactly are you trying to get hold of? Can you share a screenshot of the element?Thereafter
i cannot get screenshots, this in an input text field which contains a value inside but the DOM does not display the value here. So i am not able to get the input field's textUrissa
C
1

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

Use element.getAttribute("textContent") instead.

Cabrera answered 12/5, 2017 at 17:32 Comment(0)
F
1

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

element.getAttribute("placeholder");
Fagoting answered 25/10, 2017 at 21:2 Comment(0)
H
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')
Horoscopy answered 28/8, 2018 at 19:25 Comment(1)
In Firefox (at least 106.0.2), it is "Inspector" (invoke it by menu ToolsBrowser ToolsWeb 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 CopyXPath.Heartache
T
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
Terminus answered 31/10, 2019 at 0:23 Comment(1)
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...Fidole
D
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);
Didymium answered 20/4, 2020 at 14:39 Comment(0)
F
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.

Fidole answered 21/6, 2020 at 12:44 Comment(0)
H
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

Humperdinck answered 12/7, 2022 at 2:53 Comment(0)
A
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.

Alurd answered 13/3, 2023 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.