简体   繁体   中英

Cannot handle pop-up windows consistently

I have a tricky situation:

I am handling two popups after entering login info, but on the second pop-up I am not able to consistently hit the OK button every time

I have tried Waits, Storing Element in List and Switch cases

public class loginUser {

public static WebDriver driver = new InternetExplorerDriver();

public static void winHandles()
{
    String newHandle = null;
    Set<String> newHandles = driver.getWindowHandles();
    Iterator<String> itr = newHandles.iterator();
    while(itr.hasNext()){
        newHandle = itr.next();
        driver.switchTo().window(newHandle);
        System.out.println(driver.getCurrentUrl());
    }
}

public static void main(String args[]) throws InterruptedException
{

    System.setProperty("","");







    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    driver.manage().window().maximize();

    driver.get("");

    driver.findElement(By.name("")).sendKeys("");

    driver.findElement(By.name("")).sendKeys("");

    driver.findElement(By.name("")).sendKeys("");

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


    String parentWindowHandler = driver.getWindowHandle(); 

    winHandles();


    List<WebElement> yesbutton = driver.findElements(By.id("btnYes"));

    for (int i = 0; i < 4; i++) {

        if(yesbutton.get(0).isDisplayed())
        {
            yesbutton.get(0).click();
        }
        else {

            System.out.println("button is creating problems");
        }   
    }


    driver.switchTo().window(parentWindowHandler);

    System.out.println(driver.getCurrentUrl());



    winHandles();

    Thread.sleep(3000);

    List<WebElement> okbutton = driver.findElements(By.id("btnOK"));

    for (int i = 0; i < 4; i++) {

        if(okbutton.get(0).isDisplayed())
        {
            okbutton.get(0).click();
        }
        else {

            System.out.println("button is creating problems");
        }   
    }


    Thread.sleep(7000);

    winHandles();

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



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

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

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

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




    driver.close();




}}

So, this is the hard coded version The locator with id btnOK is causing problems mostly. Like 4 out of 5 times

I also have the same kind of issue while running test in firefox but in chrome, test runs successfully. You can use try catch :

try
{
  okbutton.get(0).click();
}
catch(Exception e)
{

}

This is not advisable but due to browser compatibility, i had to use this as a temporary solution.If anyone has real solution kindly update me too...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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