Video Link:
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 50 51 52 |
/* * Author: Akash Tyagi * Date: 22Sep2018 */ public class MultiWindow { public static void main(String[] args) throws InterruptedException { 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(); //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)); //perform a click operation on the 2nd tab to check the switch is completed WebElement btn_save = driver.findElement(By.xpath("//a[@title = 'Save']")); btn_save.click(); Thread.sleep(5000); //This will close only the current tab. //This is a basic difference between driver.quit and driver.close //driver close will only close the current window or tab //where as driver quit will quit all the windows open under the driver driver.close(); //Return to default window driver.switchTo().window(tabs.get(0)); //driver quit //driver.quit(); //will close all the windows } } |