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

后端 未结 7 573
情深已故
情深已故 2020-12-03 07:05

I am facing an issue in automating a web application using selenium web driver.

The webpage has a button which when clicked opens a new window. When I use the follow

7条回答
  •  攒了一身酷
    2020-12-03 07:36

    I use this to wait for window to be opened and it works for me.

    C# code:

    public static void WaitUntilNewWindowIsOpened(this RemoteWebDriver driver, int expectedNumberOfWindows, int maxRetryCount = 100)
        {
            int returnValue;
            bool boolReturnValue;
            for (var i = 0; i < maxRetryCount; Thread.Sleep(100), i++)
            {
                returnValue = driver.WindowHandles.Count;
                boolReturnValue = (returnValue == expectedNumberOfWindows ? true : false);
                if (boolReturnValue)
                {
                    return;
                }
            }
            //try one last time to check for window
            returnValue = driver.WindowHandles.Count;
            boolReturnValue = (returnValue == expectedNumberOfWindows ? true : false);
            if (!boolReturnValue)
            {
                throw new ApplicationException("New window did not open.");
            }
        }
    

    And then i call this method in the code

    Extensions.WaitUntilNewWindowIsOpened(driver, 2);
    

提交回复
热议问题