Home > OS >  Selenium : Handling modal dialog box
Selenium : Handling modal dialog box

Time:12-22

driver.get("https://www.leafground.com/alert.xhtml");
        //Alert simple
        driver.findElement(By.id("j_idt88:j_idt91")).click();
        
        Alert alert1 = driver.switchTo().alert();
        Thread.sleep(1000);
        alert1.accept();
        
        //Alert confirm
        
        driver.findElement(By.id("j_idt88:j_idt93")).click();
        Thread.sleep(100);
        Alert alert2 = driver.switchTo().alert();
        alert2.accept();
        
        //Prompt dialog
        driver.findElement(By.id("j_idt88:j_idt104")).click();
        Thread.sleep(500);
        Alert alert3 = driver.switchTo().alert();
        alert3.sendKeys("Praveen Sundaram");
        alert3.dismiss();
        
        // sweet alert 
        driver.findElement(By.id("j_idt88:j_idt95")).click();
        String s = driver.findElement(By.className("ui-dialog-titlebar")).getText();
        System.out.println(s);
        driver.findElement(By.id("j_idt88:j_idt98")).click();
        
        //Sweet alert confirmation
        
        driver.findElement(By.id("j_idt88:j_idt106")).click();
        driver.findElement(By.id("j_idt88:j_idt108")).click();
        
        
        //modal
        driver.findElement(By.id("j_idt88:j_idt100")).click();
        driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[5]/div[2]/form[1]/div[1]/div[1]/div[4]/div[1]/div[1]/a[1]/span[1]")).click();
        

Everything passes except the last modal dialog box. It has only 'X' button to close the box. I was able to locate the element but when I perform click operation. it is not closing the box. I even checked whether the 'x' is enabled or not.

CodePudding user response:

Two things you need to consider here.

  1. You need to wait for model popup to appear, you can user either implicit wait or explicit wait or sleep method.
  2. absolute xpath is always fragile, always try to use relative xpath to uniquely identify the element.

code:

driver.findElement(By.id("j_idt88:j_idt100")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//div[@id='j_idt88:j_idt101']//span[@class='ui-icon ui-icon-closethick']")).click();
  • Related