C#C
C#2y ago
1 reply
uselessxp

Selenium wait for the page to load completely

Even if I followed documentation sample, I'm still getting:
OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#username"}
  (Session info: chrome=122.0.6261.112); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception'

Below my code:
private void button2_Click(object sender, EventArgs e)
{
    driver.Navigate().GoToUrl("https://mywebsiste.com/login");

    // Wait for the page to load completely
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10))
    {
        PollingInterval = TimeSpan.FromMilliseconds(300),
    };
    wait.IgnoreExceptionTypes(typeof(NoSuchElementException));

    wait.Until(d =>
    {
        // Login
        driver.FindElement(By.Id("username")).SendKeys("myusername");
        driver.FindElement(By.Id("password")).SendKeys("mypassword");
        driver.FindElement(By.ClassName("remember")).Click();
        driver.FindElement(By.Id("loginSubmit")).Click();
        return true;
    });
}

What am I doing wrong?
Was this page helpful?