2017-08-03 81 views
1

我正在写一个selenium脚本来登录并创建新邮件,发送它并注销。但是当我点击新邮件按钮时,它会打开一个新窗口。在硒中我如何处理这个问题。我对硒很陌生。请详细解释。在Selenium中如何处理新窗口?

+3

的[如何处理在硒webdriver的复式窗口,需要从第二到第三窗口切换](https://stackoverflow.com/questions/45455402/how-to-handle-mutliple-windows可能的复制-in-selenium-webdriver-need-to-switch-from-second) – DebanjanB

回答

0

使用下面的代码,你必须使用getWindowHandles-我希望它能帮助,让我知道如果你被卡住其他地方 -

@Test 
     public void multipleWindows() { 
      driver.get(URL+"/windows"); 
      driver.findElement(By.cssSelector(".example a")).click(); 
      Object[] allWindows = driver.getWindowHandles().toArray(); 
      driver.switchTo().window(allWindows[0].toString()); 
      Assert.assertNotEquals(driver.getTitle(), "New Window"); 
      driver.switchTo().window(allWindows[1].toString()); 
      Assert.assertEquals(driver.getTitle(), "New Window"); 
     } 
    } 
0

试试看这个代码,很容易理解。

WebDriver driver = new FirefoxDriver(); 
driver.get("http://demo.guru99.com/popup.php"); 

driver.findElement(By.xpath("html/body/p/a")).click(); 

// return the parent window name as a String 
String parentWindow=driver.getWindowHandle(); 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 


// Pass a window handle to the other window 
for(String childWindow: driver.getWindowHandles()) 
    { 
     System.out.println("Switch to child window"); 

     //switch to child window 
     driver.switchTo().window(childWindow); 

     //find an element and print text of it 
    WebElement textLabel=driver.findElement(By.xpath("html/body/div[1]/h2")); 
    System.out.println(" text: "+textLabel.getText()); 
    driver.close(); 
      } 
    System.out.println("Get back to parent window"); 

     //switch to Parent window 
     driver.switchTo().window(parentWindow); 

    //find an element and print text of it 
    WebElement logotext=driver.findElement(By.xpath("html/body/div[1]/h2")); 
     System.out.println("text: "+logotext.getText()); 
     driver.close(); 
     } 
相关问题