Rehyn
Rehyn

Reputation: 3

Selenium: Entering Username + Password

I'm currently writing a piece of testing software using C# and Selenium. I'm trying to input a username and password and use the login feature of the website but everytime the form is submitted it comes back as an invalid username or password, I believe that it's trying to enter the data either duplicate times or entering data in the wrong field.

This is my code:

        private static void LogInAsUser()
    {
        Driver.FindElement(By.XPath("//a[contains(@href, '#login-window')]")).Click();
        Driver.FindElement(By.XPath("//input[contains(@id, 'UserName')]")).Click();
        Driver.FindElement(By.XPath("//input[contains(@id, 'UserName')]" )).SendKeys("[email protected]");
        Driver.FindElement(By.XPath("//input[contains(@id, 'Password')]")).Click();
        Driver.FindElement(By.XPath("//input[contains(@id, 'Password')]")).SendKeys("example");
        Driver.FindElement(By.XPath("//input[contains(@id, 'Password')]")).Submit();
        //Driver.FindElement(By.Id("btnLogin")).Submit(); //*[@id="btnLogin"] //*[@id="login-window"]/div/div/div[3]/div[1]/div
        {
        Result.Pass(60);
        }
    }

Not the cleanest of code I know but any help would be appreciated.

Upvotes: 0

Views: 10319

Answers (1)

drkthng
drkthng

Reputation: 6939

Would be helpful if you posted your HTML code, but even without it try it this way:

private static void LogInAsUser()
{
        Driver.FindElement(By.XPath("//a[contains(@href, '#login-window')]")).Click();

        IWebElement username = Driver.FindElement(By.XPath("//input[contains(@id, 'UserName')]" ));
        IWebElement password = Driver.FindElement(By.XPath("//input[contains(@id, 'Password')]"));

        username.Clear();
        username.SendKeys("[email protected]");

        password.Clear();
        password.SendKeys("example");

        Driver.FindElement(By.Id("btnLogin")).Click();
        {
            Result.Pass(60);
        }
}

You don't need the Click() methods for your input elements, just find them and send the data. You only want to click the Login Button at the end.

Upvotes: 1

Related Questions