Selenium webdriver window handles c# switchTo failed

后端 未结 5 1246
广开言路
广开言路 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: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 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);
    

提交回复
热议问题