How can selenium web driver get to know when the new window has opened and then resume its execution

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://stackoverflow.com/questions/9188343/how-can-selenium-web-driver-get-to-know-when-the-new-window-has-opened-and-then


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???

share | improve this question

75% accept rate
 
feedback

2 Answers

up vote 2 down vote accepted

You need to switch the control to pop-up window before doing any operations in it.

In this way you can solve your problem

before opening the popup window get the handle of main window and save it.

String mwh=driver.getWindowHandle();

Now try to open the popup window by performing some action:

driver.findElement(By.xpath("")).click();

Set s=driver.getWindowHandles();
//this method will you handle of all opened windows

Iterator ite=s.iterator();

while(ite.hasNext())
{
    String popupHandle=ite.next().toString();
    if(!popupHandle.contains(mwh))
    {
                driver.switchTo().window(popupHandle);
                /**/here you can perform operation in pop-up window**
                //After finished your operation in pop-up just select the main window again
                driver.switchTo().window(mwh);
    }
}
share | improve this answer
 
Thanks for the reply.It is working. –  Ozone Feb 14 at 4:45
feedback

You could wait until the operation succeeds e.g., in Python:

from selenium.common.exceptions    import NoSuchWindowException
from selenium.webdriver.support.ui import WebDriverWait

def found_window(name):
    def predicate(driver):
        try: driver.switch_to_window(name)
        except NoSuchWindowException:
             return False
        else:
             return True # found window
    return predicate

driver.find_element_by_id("id of the button that opens new window").click()        
WebDriverWait(driver, timeout=50).until(found_window("new window name"))
WebDriverWait(driver, timeout=10).until( # wait until the button is available
    lambda x: x.find_element_by_id("id of button present on newly opened window"))\
    .click()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值