Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get inner text of an element in Selenium?

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.

like image 237
Bishwaroop Chakraborty Avatar asked May 12 '17 09:05

Bishwaroop Chakraborty


People also ask

How do I fetch inner text of an element in Selenium?

The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.

How do I extract text from web element?

We can get text from a webelement with Selenium webdriver. The getText() methods obtains the innerText of an element. It fetches the text of an element which is visible along with its sub elements. It ignores the trailing and leading spaces.


3 Answers

try this

     WebElement element= driver.findElement(By.id("id value"));  
     String val=element.getAttribute("innerText")
like image 160
Eknath Avatar answered Nov 03 '22 10:11

Eknath


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')
like image 34
Stipe Avatar answered Nov 03 '22 09:11

Stipe


This input tag is disabled, hence element.getText() return blank val. Use element.getAttribute("textContent") instead. Hope this will work.

like image 41
Harsh Avatar answered Nov 03 '22 10:11

Harsh