Opening new window in WebDriver using C#

后端 未结 2 1295
情深已故
情深已故 2021-01-03 09:31

EDIT 4:

\"enter EDIT 3

相关标签:
2条回答
  • 2021-01-03 09:35
    GoToMysiteUrl();
    IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
    addtoList.Click();
    

    // Post above operation a new window would open as described in problem

    // Get hold of Main window's handle

    string  currentWindow = Driver.CurrentWindowHandle;
    

    // Switch to the newly opened window

    Driver.SwitchTo().Window("Your Window Name");
    

    // Perform required Actions/Assertions here and close the window

    // Switch to Main window

    Driver.SwitchTo().Window(currentWindow);
    
    0 讨论(0)
  • 2021-01-03 09:54

    The piece that most people miss when dealing with popup windows in IE is that a click on an element is asynchronous. That is to say, if you check the .WindowHandles property immediately after a click, you may lose the race condition, because you're checking for the existence of a new window before IE has had the chance to create it, and the driver has had a chance to register it exists.

    Here's the C# code I would use to perform the same operation:

    string foundHandle = null;
    string originalWindowHandle = driver.CurrentWindowHandle;
    
    // Get the list of existing window handles.
    IList<string> existingHandles = driver.WindowHandles;
    IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
    addtoList.Click();
    
    // Use a timeout. Alternatively, you could use a WebDriverWait
    // for this operation.
    DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(5));
    while(DateTime.Now < timeout)
    {
        // This method uses LINQ, so it presupposes you are running on
        // .NET 3.5 or above. Alternatively, it's possible to do this
        // without LINQ, but the code is more verbose.
        IList<string> currentHandles = driver.WindowHandles;
        IList<string> differentHandles = currentHandles.Except(existingHandles).ToList();
        if (differentHandles.Count > 0)
        {
            // There will ordinarily only be one handle in this list,
            // so it should be safe to return the first one here.
            foundHandle = differentHandles[0];
            break;
        }
    
        // Sleep for a very short period of time to prevent starving the driver thread.
        System.Threading.Thread.Sleep(250);
    }
    
    if (string.IsNullOrEmpty(foundHandle))
    {
        throw new Exception("didn't find popup window within timeout");
    }
    
    driver.SwitchToWindow(foundHandle);
    
    // Do whatever verification on the popup window you need to, then...
    driver.Close();
    
    // And switch back to the original window handle.
    driver.SwitchToWindow(originalWindowHandle);
    

    Incidentally, if you're using the .NET bindings, you have access to a PopupWindowFinder class in the WebDriver.Support.dll assembly, which uses a very similar approach to the locating popup windows. You may find that class meets your needs exactly, and can use it without modification.

    0 讨论(0)
提交回复
热议问题