Try catch in Selenium

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class TrycatchUnderstand {
    public WebDriver driver = new ChromeDriver();
    @Test
    public void tcu(){
       
            //WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
        try{   
            driver.findElement(By.xpath("//input[@type='submit'][@name='proceed']")).click();
            driver.findElement(By.xpath("//a[text()='Home']")).click();
            driver.close();
        }
        catch(UnhandledAlertException uae){
            System.out.println("Alert has ocured: "+uae);
            Alert al = driver.switchTo().alert();
            al.accept();
            driver.switchTo().defaultContent();
        }
        finally{
            driver.findElement(By.xpath("//a[text()='Home']")).click();
            driver.close();
        }
       
    }

}

No comments:

Post a Comment