how to handle alert prompt dialog box using selenium webdriver ?

package SeleniumLearn.Com;

import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class AlertOk {
     public WebDriver driver;
     
@Test
            public void Alert()throws Exception {
            driver.get("http://www.seleniumlearn.com/how-handle-alert-box-using-selenium");
            driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//*[@id='node-100']/div/div[1]/div/div/button")).click();
            Thread.sleep(5000);
            driver.switchTo().alert().accept();    
            }
@BeforeTest
            public void beforeTest() {
            driver= new FirefoxDriver();
            }
}

Tags: