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;"/>
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.
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.
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.
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.
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.
Java
ele.getAttribute("innerHTML");
This could get the text already in the background and not displayed on the page yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With