i am facing below issue in automating web application using selenium web driver.
Webpage has a button which when clicked opens new window.when i use the below code, it throws "OpenQA.Selenium.NoSuchWindowException: No window found"
WebDriver.FindElement(By.Id("id of the button that opens new window")).Click();
//Switch to new window
_WebDriver.SwitchTo().Window("new window name");
//Click on button present on the newly opened window
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click();
To solve above issue i used wait between button click and switch statements
WebDriver.FindElement(By.Id("id of the button that opens new window")).Click();
Thread.Sleep(50000); //wait
//Switch to new window
_WebDriver.SwitchTo().Window("new window name");
//Click on button present on the newly opened window
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click();
This solved the issue. But i do not want to use "Thread.Sleep(50000);" statement becauseif the window takes more time to open, code can fail and if window opens quickly then it makes the test slow unnecessarily. Is there any way to know when the window has opened and then the test can resume its execution???