2017-08-03 84 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(); 
     } 
相關問題