Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get entered text from a textbox in Selenium

I enter a value in TextBox or a Combobox, and would like to retrieve the value I have just entered. I see that Selenium Weblement method getText() doesn't retrieve the value, it seems the entered text doesn't get pushed into DOM.

Any solutions?

like image 694
Fazy Avatar asked Dec 21 '12 12:12

Fazy


People also ask

What is enter values into text boxes in Selenium?

sendkeys in Seleniumsendkeys() in Selenium is a method used to enter editable content in the text and password fields during test execution.

How do you press Enter in Selenium after entering text?

You can simulate hit Enter key by adding "\n" to the entered text. For example textField. sendKeys("text you type into field" + "\n") .

Can we enter text into textbox without using sendKeys ()?

We can input text in the text box without the method sendKeys with thehelp of the JavaScript Executor. Selenium executes JavaScript commands with the help of the executeScript method. The JavaScript command to be run is passed as parameter to the method.

How do you getText from a label in Selenium?

For getting the label text we use getText method. This method returns a string value. String labelText = driver. findElement(By.id("usernamelbl")).


1 Answers

The getText() method is for retrieving a text node between element tags for example:

<p>Something</p> 

getText() will return "Something"

In a textbox typed text goes into the value attribute so you can try something like:

findElement(By.id("someid")).getAttribute("value"); 

ComboBox is a bit different. But if you're using the Select object you can use the method:

Select selectItem = new Select(findElement(By.id("someid"))); selectItem.getFirstSelectedOption().getText(); 
like image 62
Bob Paulin Avatar answered Sep 21 '22 19:09

Bob Paulin