9

I want to getText() using By.id or By.cssSelector.

I managed to solve my problem by doing getAttribute("value"), but I don't understand why getText() doesn't work like I expect it, and I might need it so all help is appreciated.

Here is the Java code:

WebDriverWait wait = new WebDriverWait(driver, 10);
Boolean elementIsPresent = wait.until(ExpectedConditions.textToBePresentInElementValue(By.cssSelector("#general_service_name"),"[reg] general_service_name")); // true

//WebElement general_service_name = driver.findElement(By.cssSelector("#general_service_name"));
WebElement general_service_name = driver.findElement(By.id("general_service_name"));

// Display check
Boolean isDisplayed;
if(general_service_name.isDisplayed())  isDisplayed = new Boolean(true); else isDisplayed = false; //true

String text_empty = general_service_name.getText(); //""
String text_with_value = driver.findElement(By.id("general_service_name")).getAttribute("value"); //"[reg] general_service_name"

And HTML:

<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Lpgfmk
  • 391
  • 1
  • 4
  • 17
  • 2
    getText returns only visible text and does NOT return any HTML elements and does NOT return any hidden text. For example, with Java I was having this issue with a BR tag returning empty. Instead of `.getText()` I used `.getAttribute("innerHTML")` which will then return what I was looking for, including any HTML that is invisible or text that is hidden. – jsherk Jun 09 '14 at 16:15
  • @jsherk: But that is not what *this* question is about(?). – Peter Mortensen Nov 24 '20 at 19:52
  • @PeterMortensen - Well yes it sort of is because the op said "but I don't understand why getText() doesn't work like I expect it" and the reason is because getText returns text only with no HTML elements whereas getAttribute will return the html elements along with the text. But I did respond as a comment as opposed to an answer because its only partially what the question is about. – jsherk Nov 25 '20 at 20:05

5 Answers5

13

http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getText()

getText() delivers the innerText of a WebElement.

Your input field does not have any inner Text. The text is found inside your value-attribute, hence accessing it via getAttribute("value") is the correct way to do it.

azraelAT
  • 752
  • 5
  • 9
4

Java ele.getAttribute("innerHTML");

This could get the text already in the background and not displayed on the page yet.

Boyka Zhu
  • 391
  • 1
  • 4
  • 18
  • This was really helpful in a situation where getText() returned an empty string but your solution returned the inner Text of the element. Thanks ! – Chris May 16 '17 at 13:11
0

Simple answer - it's designed this way. getText() parses the content of the tag (i.e. its innerText), which is obviously empty for inputs.

Danstahr
  • 4,190
  • 22
  • 38
0

You may use this if you want to search for a given text on a WebElement. Pass it directly or through a string:

String textoToSearch = "Text inside Webelement";
driver.findElement(By.id("someID).getText().contains("textToSearch");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SeekanDestroy
  • 511
  • 1
  • 5
  • 12
0

getText()

getText() returns the visible text of this element.

java.lang.String getText()
Get the visible (i.e. not hidden by CSS) text of this element, including sub-elements.


Returns:
The visible text of this element.

As per the HTML of the element:

<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>

The WebElement doesn't have a visible text but the value attribute have the value set as [reg] title.

So to extract the value of the value attribute i.e. [reg] title you can use either of the following Locator Strategies:

  • Using cssSelector:

    System.out.println(wd.findElement(By.cssSelector("input#general_service_name[name='general_service_name']")).getAttribute("value"));
    
  • Using xpath:

    System.out.println(wd.findElement(By.xpath("//input[@id='general_service_name' and @name='general_service_name']")).getAttribute("value"));
    

Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using cssSelector and getText():

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#general_service_name[name='general_service_name']"))).getAttribute("value"));
    
  • Using xpath and getAttribute("innerHTML"):

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='general_service_name' and @name='general_service_name']"))).getAttribute("value"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352