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
1,958 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,510 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
7 answers

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

Try this once driver.findElement(By.id("id")).sendKeys(Keys.CONT ...READ MORE

answered Sep 5 in Selenium by Sri
• 2,970 points
46,231 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 26, 2019 in Selenium by Abha
• 27,930 points
1,097 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,510 points
3,522 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,526 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,591 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
2,851 views
0 votes
1 answer