How to open a link in new tab of chrome browser using Selenium WebDriver

0 votes
System.setProperty("webdriver.chrome.driver", "D:\\softwares\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
driver.findElement(By.linkText("www.facebook.com")).sendKeys(selectLinkOpeninNewTab);
Aug 9, 2018 in Selenium by Perry
• 17,100 points
103,091 views
i have write same code but url not open
Have you tried Abha's solution?

8 answers to this question.

0 votes

Below code worked fine for me:

    driver = new ChromeDriver();
    driver.manage().window().maximize();

    baseUrl = "http://www.google.co.uk/";
    driver.get(baseUrl);
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs.get(1)); //switches to new tab
    driver.get("https://www.facebook.com");

    driver.switchTo().window(tabs.get(0)); // switch back to main screen        
    driver.get("https://www.news.google.com");

For further understanding, you can refer to the Selenium online training.

answered Aug 9, 2018 by Meci Matt
• 9,460 points

This code is not working for me

Hey Sonal, try this piece of code to open a link in a new tab:

driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
String a = "window.open(link,'_blank');";  // replace link with your desired link
((JavascriptExecutor)driver).executeScript(a);

Hope this would help you. Let me know if it works.

working....thanks
But if we want to execute code on main tab first and then secodary tab then how to do this?
driver.switchTo().window(tabs.get(0)); // switch to main screen

//execute your code.

driver.switchTo().window(tabs.get(1)); //switches to new tab

0 votes
driver.execute_script('''window.open("http://google.com","_blank");''')
answered Nov 27, 2018 by Ganesh
0 votes

First open a new tab:

driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 

Then open the url using get:

driver.get('www.facebook.com')
answered Nov 27, 2018 by Mahesh
0 votes
ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
answered Nov 27, 2018 by kartik
0 votes

You can open a new tab first. And then change the focus to the newly created tab

driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get("www.facebook.com")
answered Nov 27, 2018 by Shrikanth
0 votes
driver.switchTo().window(tabs.get(1)); or javascrpirt 
or 
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");





answered Sep 3, 2020 by Sri
• 3,190 points
0 votes

Try this once

driver.findElement(By.id("id")).sendKeys(Keys.CONTROL+"t");
answered Sep 6, 2020 by Sri
• 3,190 points
0 votes

This below code works for me in Selenium 3 and chrome version 58.

    WebDriver driver = new ChromeDriver();
    driver.get("http://yahoo.com");  
    ((JavascriptExecutor)driver).executeScript("window.open()");
    ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
    driver.switchTo().window(tabs.get(1));
    driver.get("http://google.com");
answered Dec 14, 2020 by Gitika
• 65,770 points

Related Questions In Selenium

0 votes
1 answer

How to open a new browser tab using Ruby Selenium Webdriver?

Hi Utkarsh, you can use JS Executor ...READ MORE

answered Aug 27, 2019 in Selenium by Abha
• 28,140 points
3,959 views
0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
16,071 views
0 votes
1 answer
0 votes
1 answer

How to open new tab in same browser and switch between them using Selenium?

Hi Mugdha, you can use following code ...READ MORE

answered May 24, 2019 in Selenium by Abha
• 28,140 points
22,324 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,731 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,171 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,247 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,887 views
0 votes
1 answer

Open new tab instead of a new window in Selenium WebDriver

The current version of Selenium do not ...READ MORE

answered Jun 22, 2018 in Selenium by Meci Matt
• 9,460 points
3,954 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP