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,020 points
15,568 views

5 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,420 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.

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

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

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

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,130 points
4,197 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 in Selenium by Abha
• 27,180 points
1,001 views
0 votes
1 answer

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,110 points
977 views
0 votes
1 answer
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,130 points
1,136 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
875 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,420 points
948 views