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
52,279 views
i have write same code but url not open
Have you tried Abha's solution?

7 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");
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 by Sri
• 2,970 points
0 votes

Try this once

driver.findElement(By.id("id")).sendKeys(Keys.CONTROL+"t");
answered Sep 5 by Sri
• 2,970 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 26, 2019 in Selenium by Abha
• 27,950 points
1,295 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 5 in Selenium by Sri
• 2,970 points
10,335 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
• 27,950 points
11,640 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,520 points
4,039 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,986 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,890 points
2,923 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 2 in Selenium by Sri
• 2,970 points
3,361 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
2,170 views