Selenium can only be automated on browser web elements. Opening a new tab is an operation performed in a web browser, which is a stand-alone application. You can use the Robot class from the java.util package for this. *, Which can perform operations using the keyboard, regardless of the type of application. So here is the code for your work. Please note that you cannot automate standalone applications using the Robot class, but you can perform operations with the keyboard or mouse
System.setProperty("webdriver.chrome.driver","softwares\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get("http://www.google.com"); Robot rob = new Robot(); rob.keyPress(keyEvent.VK_CONTROL); rob.keyPress(keyEvent.VK_T); rob.keyRelease(keyEvent.VK_CONTROL); rob.keyRelease(keyEvent.VK_T);
After this step, you will need a window iterator to go to a new tab:
Set <String> ids = driver.getWindowHandles(); Iterator <String> it = ids.iterator(); String currentWindow = it.next(); String newWindow = it.next(); driver.switchTo().window(newWindow); driver.findElement(By.linkText("www.facebook.com")).sendKeys(selectLinkOpeninNewTab);
source share