Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebElement value empty after sending keys

I'm running some simple form tests where values are added fields.

After each value is added to a field:

input.SendKeys(value);

I want to check the value in the field is correct. This may sound unusual but the field may have an ajax search attached and if the search doesn't pull back a match, the field will be empty.

I've tried testing the text value of the WebElement after sending the keys but it always seems to be empty:

bool match = input.Text.Equals(value);
// input.Text always seems to be an empty string

I'm using Selenium 2 with the WebDriver - is there another way to perform these checks? Is there a particular reason why the WebElement is empty even if the SendKeys successfully prints a value (actually in the browser) in to the WebElement (textbox)?

Any help would be grateful.

like image 438
Phil Cooper Avatar asked Sep 01 '11 15:09

Phil Cooper


People also ask

What happens when we send null in sendKeys?

Explanation: sendKeys method was expecting an argument of type char sequence but we pass null which is invalid. So it throws IllegalArgumentException. Only local connections are allowed. Explanation: In this case, sendKeys method will not be able to identify null before it is processed for typing.

Why sendKeys is not showing in Selenium?

If we encounter issues while working with the sendKeys method, then we can use the JavaScript Executor to input text within an edit box. Selenium can run JavaScript commands with the help of the executeScript method. JavaScript command to be used is passed as a parameter to this method.

Is sendKeys () a WebElement method?

What Is Selenium sendKeys() Method? Selenium provides sendKeys() method to input content in editable text fields or password fields in a webpage. These fields are like the typical web elements present on the web page that can be identified using any of the Selenium locators.

How do you store values in WebElement?

We can set value to input webelement using Selenium webdriver. We can take the help of the sendKeys method to enter text to the input field. The value to be entered is passed as an argument to the method.


1 Answers

It may be possible that the text value that you are entering is assigned as a "value" attribute of text box and not as "text"

input.sendKeys(enteredValue)
String retrievedText = input.getAttribute("value");
if(retrievedText.equals(enteredValue)){
 //do stuff
}
like image 61
nilesh Avatar answered Oct 02 '22 18:10

nilesh