Search code examples
c#seleniumxpathcss-selectorswebdriverwait

no such element: Unable to locate element : {"method":"css selector","selector":"#Password"} using Selenium and C#


I wrote a simple test for login

namespace Seleniumtest.Customer
{
    [TestFixture]
    class LoginTest :TestBase
    {
        [Test]
        public void LoginWithPhoneNumber()
        {
            webDriver.Navigate().GoToUrl("https://test123.ifc.ir/login");
            webDriver.FindElement(By.Id("EmailOrPhoneNumber")).SendKeys("09108599423");
            webDriver.FindElement(By.ClassName("buttons")).Click();
            var input = webDriver.FindElement(By.Id("Password"));     
            Assert.That(input.Displayed, Is.True);
        }

    }
}

This program will click login and have login and find partial view input password but its just instert the value in username and click the submit but never find password.


Solution

  • The issue is that on page one you have username and clickable button, and once you click on the button it will redirect you to a new page where you can provide the password.

    However, this new page is not rendered properly and that's the reason you are getting

    no such element: Unable to locate element : {"method":"css selector","selector":"#Password"} 
    

    In C#-Selenium bindings, you can use the explicit wait like below:

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement input = wait.Until(e => e.FindElement(By.Id("Password")));
    

    and now the input is a web element, you can input the password with SendKeys method.

    input.SendKeys("your password");
    

    Reference link