Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current text of input field using Selenium

Let's say I have this input element:

<input id="email" value="[email protected]">

I execute this piece of code:

var emailInputField = driver.FindElement(By.Id(email));
var email = emailInputField.GetAttribute("value");
emailInputField .InputField.Clear();
var empty = emailInputField.GetAttribute("value");

I would expect variable empty to be empty but it contains same text as email since I cleared the text. I realize that value attribute doesn't get synchronized with entered text, so my question is, how do I find out what text currently is in the text box?

like image 624
user1242967 Avatar asked Oct 27 '25 05:10

user1242967


1 Answers

Because you have already selected the element and assigned emailInputField to it then empty is going to refer to that. If you re-assign emailInputField after the clear and then get the value it should be empty. So :

var emailInputField = driver.FindElement(By.Id(email));
var email = emailInputField.GetAttribute("value");
emailInputField .InputField.Clear();
var empty = driver.FindElement(By.Id(email)).GetAttribute("value");
like image 165
Cathal Avatar answered Oct 29 '25 19:10

Cathal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!