1

I want to open www.google.com in one tab and then open new tab and open www.facebook.com
I am using Below code but it open facebook in first tab

driver.get("www.google.com");    
driver.findElement(By.tagName("Body")).sendKeys(keys.CONTROL + "t");//opens new tab  
driver.get("www.facebook.com");//but load facebook in first tab i.e on google page  

Is this because I am using same name driver.get??

Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
user2795446
  • 23
  • 1
  • 4

2 Answers2

1

If the code to open the new tab worked, you can use driver.switchTo().window() to switch to the newly opened tab (as seen here).

But If the tab didn't open, Selenium does not provide any mechanism to do this, so you have to implement around it (like using the java.awt.Robot class)

You should also consider just using two different instances of the WebDriver and run both your sites in its own window

Community
  • 1
  • 1
Simiil
  • 2,281
  • 1
  • 22
  • 32
0

Use java awt Robot class as below.

          driver.get("http://www.google.com");    
          Robot r = new Robot();        
          r.keyPress(KeyEvent.VK_CONTROL);
          r.keyPress(KeyEvent.VK_T);
          r.keyRelease(KeyEvent.VK_CONTROL);
          r.keyRelease(KeyEvent.VK_T);
          driver.get("http://www.facebook.com");
HemChe
  • 2,249
  • 1
  • 21
  • 33