Getting current text of an input field using Selenium
Let's say I have this input element:
<input id="email" value="some@email.com">
I am executing 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 the empty variable to be empty, but it contains the same text as the email , since I cleared the text. I understand that the value attribute is not in sync with the entered text, so my question is, how do I know what text is currently in the textbox?
+3
source to share
2 answers
Since you have already selected an element and assigned to it emailInputField
, then empty will refer to that. If you reassign emailInputField
after cleanup 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");
+5
source to share