We can handle the alerts very easily in selenium webdriver in Java using switchTo() method.
We can handle alerts using Alert Interface in Java Web Driver.
//Sample Java program to handle alert in selenium webdriver
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com
We can handle alerts using Alert Interface in Java Web Driver.
At first, we need to get the alert reference using below
syntax.
Alert alert =
driver.switchTo().alert()
Then we can click on Ok button using below syntax.
alert.accept();
Then we can click on Cancel button using below syntax.
alert.dismiss();
To get the text displayed in the alert, you can use getText()
method
String text =
alert.getText();
package seleniumtest;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.*;
import
org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MainTest {
public static void main(String[] args)
{
WebDriver driver =null;
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\csharp\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
try{
driver.get("http://register.rediff.com/register/register.php");
driver.findElement(By.name("name")).sendKeys("ff89");
driver.findElement(By.name("name")).sendKeys(Keys.TAB);
Thread.sleep(3000);
//driver.findElement(By.name("passwd")).click();
driver.switchTo().alert().accept();
Thread.sleep(4000);
//driver.navigate();
//driver.navigate("http://www.google.com");
}catch(Exception e){
System.out.println(e.toString());
}
finally{
driver.close();
driver.quit();
}
}
}
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com
No comments:
Post a Comment