Webdriver sendkeys method does not enter password correctly

I am using Selenium Webdriver in Visual Studio C #. Nunit is a testing driver.

I am trying to enter my company website with my username and password.

query.SendKeys("myusername"); query.SendKeys(Keys.Tab); query.SendKeys("mypassword"); 

My code finds the username text field, enter "myusername" (example), the tabs in the password field are correct, but then, since it should enter "mypassword" in the pw text field, it is located before the user text field and adds it (showing "myusernamemypassword ")

What is the reason he will not allow me to enter a password?

+4
source share
1 answer

The SendKeys method will send characters to any WebElement object to which you called it, in this case a request. Even after moving to the next object, the key to the request element will still be sent. In my experience, using keys (like a tab) to move between items tends to be inconsistent and unreliable. Instead, try something like this:

 WebElement userNameField = driver.findElement(By.id("userNameTextBoxId")); WebElement passwordField = driver.findElement(By.id("passwordTextBoxId")); userNameField.sendKeys("myusername"); passwordField.sendKeys("mypassword"); 

If you do not have identifiers in the text fields, there are several other identifiers that you can use, such as classname and xpath (although I had a bad experience using xpath in selenium - use the identifier when possible).

+5
source

Source: https://habr.com/ru/post/1412614/


All Articles