Selenium webdriver window handles c# switchTo failed

后端 未结 5 1243
广开言路
广开言路 2021-02-10 02:11

Here comes 2 windows pop out during the testing.

my code:

string BaseWindow = driver.CurrentWindowHandle;                 
ReadOnlyCollection

        
相关标签:
5条回答
  • 2021-02-10 02:31
    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
    js.ExecuteScript("window.open()");
    String ventanaPrincipal = driver.CurrentWindowHandle;
    List<string> listWindow = new List<string>(driver.WindowHandles);
    driver.SwitchTo().Window(listWindow[1]);
    driver.Navigate().GoToUrl("http:www.google.com");
    IWebElement search = driver.FindElement(By.Name("q"));
    search.SendKeys("RPA");
    
    0 讨论(0)
  • 2021-02-10 02:34
    //switch to new window 
    driver.FindElement(By.Id("link")).Click(); 
    
    //wait for new window to open 
    Thread.Sleep(2000); 
    
    //get the current window handles 
    string popupHandle = string.Empty; 
    ReadOnlyCollection<string> windowHandles = driver.WindowHandles;  
    
    foreach (string handle in windowHandles)  
    {  
        if (handle != existingWindowHandle)  
        {  
             popupHandle = handle; break;  
        }  
    }  
    
     //switch to new window 
    driver.SwitchTo().Window(popupHandle); 
    
    //check for element on new page 
    webElement = driver.FindElement(By.Id("four04msg")); 
    if(webElement.Text == "THE CONTENT YOU REQUESTED COULDN’T BE FOUND...")  
    {  
        return false;  
    }  
    else  
    {  
        return true;  
    }  
    
     //close the new window to navigate to the previous one
    driver.close(); 
    
    //switch back to original window 
    driver.SwitchTo().Window(existingWindowHandle);
    
    0 讨论(0)
  • 2021-02-10 02:38
    1. Using the original post code.

      string existingWindowHandle = driver.CurrentWindowHandle;

      Its the first window.

    2. One important thing is:

      ReadOnlyCollection<string> windowHandles = driver.WindowHandles

      Contains the string name object, not the Windows Title Name, for example Collection windowHandles could contains:

      Not Windows Title Name as {Menu},{PopUp}
      It contains: {45e615b3-266f-4ae0-a508-e901f42a36d3},{c6010037-0be6-4842-8d38-7f37c2621e81}

    0 讨论(0)
  • 2021-02-10 02:40
    string NewWindowHandle = string.Empty;
    ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
    NewWindowHandle = windowHandles[windowHandles.Count - 1];
    driver.SwitchTo().Window(NewWindowHandle);
    
    0 讨论(0)
  • 2021-02-10 02:46
            IWebDriver popup = null;
            string mainWindow = driver.CurrentWindowHandle;
            bool foundPopupTitle = false;
            foreach (string handle in driver.WindowHandles)
            {
                popup = driver.SwitchTo().Window(handle);
                if (popup.Title.Contains(title))
                {
                    foundPopupTitle = true;
                    break;
                }
            }
    
            if (foundPopupTitle)
            {
                popup.Close();
            }
            //switch back to original window
            driver.SwitchTo().Window(mainWindow);
    
    0 讨论(0)
提交回复
热议问题