3

How do I open a new tab in IE using selenium (java) and open a url in that tab (not window)? I am using the below code to open a new tab?

driver.get("https://google.com/");

//below line of code opens a new tab but does sets control on new tab.
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL + "t");//opens new tab

// As control does not sets on new tab, the below link opens on first tab only..
driver.get("https://facebook.com/");//but load facebook in first tab i.e on google page  

Can anyone tell me, how to shift control to new tab so that the facebook link opens in that new tab.

Hi

I using Selenium Web-Driver Version 2.40 and IE 11.0

WebDriver driver = new InternetExplorerDriver(ieCapabilities);

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

  driver.get("https://google.com/");

  driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL + "t");//opens new tab

 //Store the current window handle

  String winHandleBefore = driver.getWindowHandle();

  //Perform the click operation that opens new window //Switch to new window open 

   for(String winHandle : driver.getWindowHandles()){

   driver.switchTo().window(winHandle);

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

    }

    // Perform the actions on new window

    //Close the new window, if that window no more required

   driver.close();

   //Switch back to original browser (first window)

     driver.switchTo().window(winHandleBefore);

     //continue with original browser (first window)

I am not able to open facebook on new tab of same window..

Regards Shashank Goyal

Chandra Shekhar
  • 664
  • 2
  • 9
  • 24
user3004247
  • 31
  • 1
  • 1
  • 3
  • This link explains in detail how to go about switching windows: http://stackoverflow.com/questions/9588827/webdriver-switch-to-new-browser-opened-after-click-on-button – Richard Apr 02 '14 at 18:59
  • I am asking to work on new tab , not on new window. – user3004247 Apr 02 '14 at 19:23
  • A tab is considered a new window by Selenium. – Richard Apr 02 '14 at 19:49
  • Note: AFAIK, Selenium [doesnot open new tabs, only new windows](http://stackoverflow.com/questions/11358316/selenium-web-driver-open-new-tab-instead-of-a-new-window/11358741#11358741). Your workaround will work I believe, however selenium will still treat it as a new window and you can switch to it using `driver.switchTo().window(windowHandle)` – Faiz Apr 02 '14 at 22:34
  • I am using selenium 2.40 and IE 11.0 and i am not able to open link in new tab of same window.I have provided the code above,that i am using.Can you please provide the code in java,so that i can go through this issue. – user3004247 Apr 03 '14 at 15:09

2 Answers2

1

You'll need to use

driver.switchTo().window(String)

to switch to the window that has come up just as you would with opening a new window.

ddavison
  • 28,221
  • 15
  • 85
  • 110
0
ArrayList<String> tabHandles1 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabHandles1.get(index));

u can pass the place the value of index or can iterate to all the tab using arrayList tabhandles1

Ravi
  • 719
  • 8
  • 23