I tried to open a new website when a new tab is opened using Selenium Webdriver

0 votes

I was opening a new browser tab through Selenium Webdriver but the new tab I opened was blank. I tried to pass the website link to the Webdriver object, but the link is still not opening.

Here is my code:
// Assuming path is already set
    WebDriver driver = new ChromeDriver();
    String url1 = "https://www.facebook.com/";
    driver.get(url1);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    Robot r = new Robot();
    r.keyPress(KeyEvent.VK_CONTROL);
    r.keyPress(KeyEvent.VK_T);
    r.keyPress(KeyEvent.VK_CONTROL);
    r.keyPress(KeyEvent.VK_T);

    Set<String> s1 = driver.getWindowHandles();

    Iterator itr = s1.iterator();
    String parent_window = (String) itr.next();
    System.out.println(parent_window + "....." + driver.getTitle());

    String child_window = (String) itr.next();
    driver.switchTo().window(child_window);
    driver.navigate().to("https://www.facebook.com/");

Mar 27, 2018 in Selenium by rajteja
4,457 views

1 answer to this question.

0 votes

Try this code:

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

    driver.switchTo().window(tabs.get(0)); // switch back to main screen        
    driver.get("https://www.google.com");
answered Mar 27, 2018 by nsv999
• 5,500 points
Hi,

What would be the same thing using javascript?

Hello @D.Gupta,

You can do it using Javascript Executor i.e executeScript() method of Javascript Executor to open a new tab in existing browser:

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://www.facebook.com/");

String link = "window.open('https://www.edureka.co','_blank');";

((JavascriptExecutor)driver).executeScript(link);

Thread.sleep(5000);

ArrayList<String> tabs = driver.getWindowHandles();

driver.switchTo().window(tabs.getIndex(0));

Hope it helps!!

ThanK you!!

Related Questions In Selenium

0 votes
8 answers

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

This below code works for me in ...READ MORE

answered Dec 14, 2020 in Selenium by Gitika
• 65,770 points
103,120 views
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,968 views
0 votes
1 answer

How to open a new tab using Selenium WebDriver?

The code below will open the link ...READ MORE

answered Nov 27, 2020 in Selenium by Gitika
• 65,770 points
914 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,764 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,184 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,258 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,896 views
0 votes
1 answer
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