C
C#4mo ago
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'
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;
});
}
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?
1 Reply
uselessxp
uselessxp4mo ago
For some reason looks like that the implicit way:
private void button2_Click(object sender, EventArgs e)
{
driver.Url = "https://mywebsiste.com/login";

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

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();
}
private void button2_Click(object sender, EventArgs e)
{
driver.Url = "https://mywebsiste.com/login";

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

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();
}
Works perfectly, also if I don't have idea about how I randomly decited to use this method, for go on, thinking that driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); just add a 10 seconds pause before execute next commands, but looks like that this one "wait max 10 seconds" but if he can't perform operations before, it do it So I suppose that it automatically handle the situation