1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public class AlertHandling { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "D:\\VisionITWorkspace\\dependencies\\chromedriver_win32\\chromedriver.exe"); //1 Create Driver object WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Navigate and click on the link driver.get("https://www.w3schools.com/jsref/met_win_alert.asp"); WebElement lnk_try_it_urself = driver.findElement(By.partialLinkText("Try it Yourself")); //this link will open new tab because of its implementation of <a target= "_blank"> lnk_try_it_urself.click(); //Switch to New tab //Get windows handles ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); //Two handle will be returned System.out.println(tabs.size()); //Switch to 2nd tab driver.switchTo().window(tabs.get(1)); //Switch to Frame because Choose from File is present in the frame WebElement frame = driver.findElement(By.id("iframeResult")); driver.switchTo().frame(frame); //Click on Try it WebElement btn_try_it = driver.findElement(By.xpath("//button[contains(text(),'Try it')]")); btn_try_it.click(); //TO slow down the Execution Thread.sleep(5000); //To click on Ok driver.switchTo().alert().accept(); //USe Dismiss to Click on cancel button //https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_confirm driver.switchTo().alert().dismiss(); // to cancel driver.switchTo().alert().sendKeys("Akash"); // to send keys } } |