简体   繁体   English

如何在不确定的时间显示弹出窗口?

[英]How to handle a pop-up window when that shows up at uncertain time?

I am trying to automate test cases. 我试图自动化测试用例。 It's difficult since the pop message appears at uncertain time as a result the test case fails. 由于弹出消息在不确定的时间出现,因此测试用例失败,这很困难。 Sometimes pop-up appears without a click and other times it is 5-6 clicks before the pop-up appears. 有时弹出窗口会在没有单击的情况下显示,有时则会在弹出窗口显示之前单击5-6次。 I can't locate the pop-up there is no id or XPath. 我找不到弹出窗口没有id或XPath。

If popup is windows based then use AutoIT library. 如果弹出窗口是基于Windows的,则使用AutoIT库。 If it is web popup then you can handle it by following code 如果它是Web弹出窗口,那么您可以通过以下代码处理它

Set<String> set =  driver.getWindowHandles();
List<String> list = new ArrayList<>(set);

// store your main window handle in variable
String mainWindow = list.get(0);

// To close all unwanted popup
for(int i =1; i <list.size(); i++)
{
    String unwantedPopup = list.get(i);
    driver.switchTo().window(unwantedPopup);
    driver.close();
}

// Switch back to your main window
driver.switchTo().window(mainWindow);

I found solution that works for me. 我找到了适合我的解决方案。

    Thread.sleep(5000); //wait for the modal message to appear
    String winHandleBefore = driver.getWindowHandle();
    driver.findElement(By.xpath("xpath")).click();
    Thread.sleep(2000);
    driver.switchTo().window(winHandleBefore);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM