2017-05-12 52 views
0

我試圖檢查我想打開的彈出窗口是否打開。如何檢查彈出窗口是否打開使用Java中的Selenium WebDriver

我也查了一些問題的答案就像

How would you check if a popup window exists using selenium webdriver?

但是,沒有任何事情幫助解決這個問題。

在這裏,首先我通過點擊登錄按鈕打開登錄窗口。

driver.findElement(By.xpath("//a[@id='login_btn']")).click(); // Click Login Button 

login

我甚至嘗試getPageSource()但是,似乎不工作。

任何形式的幫助,將不勝感激。

在此先感謝。 :)

+0

可能它不是彈出窗口;它只是一個火雞。 – kushal

回答

0
String mwh=driver.getWindowHandle(); 

現在嘗試通過執行一些動作來打開彈出窗口:

driver.findElement(By.xpath("")).click(); 

Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows 

Iterator ite=s.iterator(); 

while(ite.hasNext()) 
{ 
    String popupHandle=ite.next().toString(); 
    if(!popupHandle.contains(mwh)) 
    { 
     driver.switchTo().window(popupHandle); 
     /**/here you can perform operation in pop-up window** 
     //After finished your operation in pop-up just select the main window again 
     driver.switchTo().window(mwh); 
    } 
} 
1

如果是本地人,瀏覽器警報(=一個彈出),你可以做到以下幾點:

try{ 
    driver.switchTo().alert(); 
    // If it reaches here, it found a popup 
} catch(NoALertPresentException e){} 

它實際上處理的是一個iframe,您可以在獲取「iframe」屬性的屬性值後執行以下操作:

driver.switchTo.frame("ValueOfIframe"); 
// Treat as normal webpage. Now in iframe scope 
driver.switchTo.defaultContent(); // To return back to normal page scope 
相關問題