I’m using Selenium Webdriver on Visual Studio C# language. Nunit is the testing driver.
I’m trying to log into my companies website with my username and password.
query.SendKeys("myusername");
query.SendKeys(Keys.Tab);
query.SendKeys("mypassword");
My code locates the username text box, enter “myusername” (example), tabs to the password box correctly, but then as it should enter “mypassword” into the pw textbox, its located up to the username textbox and adds on (showing “myusernamemypassword”)
What is the reason it won’t let me enter the password?
The SendKeys method is going to send characters to whichever WebElement object you called it on, in this case, query. Even after tabbing to the next object, it’ll still be sending keys to the query element. In my experience, using keys (such as tab) to jump between elements tends to be inconsistent and unreliable. Instead, try something like this:
If you don’t have ids on the text boxes, there are several other identifiers you can use such as classname and xpath (although I’ve had bad experience with xpath in selenium — use id’s whenever possible).