How to get inner text of an element in Selenium?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 23:02:36

问题


I have an element in DOM as

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

This text box contains some text which does not reflect in this element tag. How do I get this value? Like, obviously,

element.getText() does not work and return blank. So is there a way I can get the text?

Any suggestions would be of great help.


回答1:


try this

 WebElement element= driver.findElement(By.id("id value"));  
 String val=element.getAttribute("innerText")



回答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");




回答3:


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




回答4:


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

element.getAttribute("placeholder");



回答5:


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



回答6:


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


来源:https://stackoverflow.com/questions/43933778/how-to-get-inner-text-of-an-element-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!