How to open new tab in chromedriver? The following code is not working for chrome driver. Any other ways


Driver().findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");

Add Comment
4 Answer(s)

public class NewTab {
public static void main(String[] args) throws AWTException, InterruptedException {

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“https://www.google.co.in/?gfe_rd=cr&ei=dGKEU8HNCqeOiAenwoDgAQ”);
Actions act = new Actions(driver);
WebElement link = driver.findElement(By.xpath(“//a[text()=’Gmail’]”));
act.moveToElement(link).contextClick().sendKeys(“T”).perform(); //open link in new tab
act.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();//switch to new tab by pressing control+tab
Thread.sleep(3000); // simply wait for 3sec to see new tab open or not
String main = driver.getWindowHandle();
driver.switchTo().window(main);//switch control to new tab
//act.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform(); //press control+f4 to close tab
driver.findElement(By.xpath(“//input[@name=’Email’]”)).sendKeys(“hello”); //click on a ‘Show all posts’ link of main tab to confirm we are in main tab
}
}

amitjain Supporter Answered on March 13, 2015.
Add Comment

Hi amit,
Thanks for your reply.
Using  “Actions” is not working for my code. The  following line is working with firefox driver not with chrome driver.

act.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform().

I used the following javascript code to open the new tab

JavascriptExecutor jse = (JavascriptExecutor)browser.getDriver();
jse.executeScript(“var w = window.open(); w.document.open();”);

It worked. The problem here is even after i switch the control to new tab (driver.switchto().window(“new tab handle”)) , the actions are happening in my old tab.. any specific reasons??

apvigneshkumar Default Answered on March 13, 2015.
Add Comment

Hey,

I’m facing the same problem i.e. The problem here is even after i switch the control to new tab (driver.switchto().window(“new tab handle”)) , the actions are happening in my old tab.

Do you have any solution for that? Also How we can switch back to old tab in Chrome.

SURBHI1903 Default Answered on May 25, 2016.
Add Comment

Chrome driver for selenium – 2.25
Google Chrome version – 54.0.2840

// Open link in same window but in next tab
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
driver.findElement(By.xpath(“Your Xpath Here”)).sendKeys(selectLinkOpeninNewTab);

// Switch focus to next tab
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);

Below statement to change the focus to the new tab does not work on Chrome browser but it works for Firefox.
Actions act = new Actions(driver);
act.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform(); //switch to new tab by pressing control+tab

Therefore, use the code provided above using Robot class from awt package.

shahsid84 Default Answered on November 2, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.