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???
本文解决使用Selenium WebDriver自动化测试时,在点击按钮后快速打开新窗口导致的NoSuchWindowException问题。通过在操作之间引入等待或使用特定方法获取新窗口句柄,可以确保代码正确执行。







