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
814 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,070 points

Related Questions In Selenium

0 votes
5 answers
0 votes
1 answer

Is it possible for a website to detect that we are using Selenium with ChromeDriver

Selenium tests for pre-defined javascript variables which ...READ MORE

answered Apr 27, 2018 in Selenium by Meci Matt
• 9,420 points
2,067 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,070 points
445 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
793 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
435 views
0 votes
1 answer
0 votes
1 answer