Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium getText

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;"/>
like image 285
Lpgfmk Avatar asked May 22 '14 10:05

Lpgfmk


People also ask

What is getText in Selenium?

What Is getText() Method? 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.

What is the purpose of getText () and getAttribute ()?

The getText() method simply returns the visible text present between the start and end tags (which is not hidden by CSS). The getAttribute() method on the other hand identifies and fetches the key-value pairs of attributes within the HTML tags.

How do I use Selenium to text?

Text() Method of SeleniumUsing text method of selenium web driver, find the web element with text – Write and Earn. Validate if the selected element is displayed on the web page. If it is displayed, print the text as Element found using text. If the element is not displayed, print the text as Element not found.

How do I getText from WebElement?

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.


2 Answers

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.

like image 60
azraelAT Avatar answered Sep 29 '22 08:09

azraelAT


Java ele.getAttribute("innerHTML");

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

like image 28
Boyka Zhu Avatar answered Sep 29 '22 09:09

Boyka Zhu