Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getText() returns empty string after sendKeys()

Tags:

java

selenium

I have a class with the following public instance variable

@FindBy(id="titleInput")
public WebElement titleInputBox;

Then I use page factory in the constructor to initialize it on every use

PageFactory.initElements(driver, this);

In my test case for this page, I use the following code to test whether the text I sent is actually getting set in the field ...

subtitleInputBox.sendKeys("Test");
subtitleInputBox.getText();

and I get empty string

any idea why this happens ... I think it works fine if driver.findElement() is used directly without @FindBy and PageFactory

like image 638
Chandrachud Nanduri Avatar asked Sep 06 '25 19:09

Chandrachud Nanduri


1 Answers

To get the text out of an input box like text or textarea you need to use the getAttribute("value") method. getText() works for tags like div, span etc etc.

subtitleInputBox.getAttribute("value");
like image 159
Grasshopper Avatar answered Sep 09 '25 17:09

Grasshopper