getText() of the popup or alert

getText() method is used to get the text of the popup or alert message on the browser.  getText will return the text which appears on the dialog box or alert message window.

CODE:
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

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

System.setProperty("webdriver.chrome.driver", "C://Selenium_Training//chromedriver.exe");
WebDriver driver=new ChromeDriver();


//Launching qafreaks website and click the try it button for popup alert
driver.get("http://www.qafreaks.com/p/example-for-javascript-to-handle-alerts.html");
WebElement element = driver.findElement(By.id("tryit"));
 
//Printing the element text on the console
System.out.println(element.getText());
element.click();
 
Thread.sleep(10000); 
    
//Handling alert to accept
//driver.switchTo().alert().accept();
    
//Cancelling the alert
Alert alert = driver.switchTo().alert();
alert.dismiss();  
}
}

No comments:

Post a Comment

Socialize It and Share the post with your friends
SOCIALIZE IT →
FOLLOW US →
SHARE IT →