Selenium getText

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

I managed to solve my problem by doing getAttribute ("value"), but I donโ€™t understand why getText () is not working as I expect, and I might need any help to be appreciated.

Here is java:

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;"/> 
+6
source share
3 answers

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

getText () provides the inner text of a WebElement.

There is no internal text in the input field. The text is inside your value attribute, so accessing it through getAttribute ("value") is the correct way to execute it.

+9
source

Java ele.getAttribute("innerHTML");

This may get the text already in the background and not display on the page.

+2
source

The simple answer is that it is designed that way. getText() parses the contents of the tag (i.e. its inner text), which is obviously empty for inputs.

0
source

Source: https://habr.com/ru/post/969690/


All Articles