2

I'm trying to switch the tab and do operation by navigating to url'http://toolsqa.com/' and then to Demo Sites--> E-Commerce Demo Site. Once this link is clicked a new tab in the same browser with url 'http://store.demoqa.com/' is opened. Here i tried many ways for switching the focus to the newly opened tab. But unable to do it, getting unable to identify element error while trying to do any operations in new tab.

Different codes used:

code1:

 public void switchWindow(String originalWindowKey){
  
  System.out.println("Ori"+originalWindowKey);
  Set<String> multipleWindowHandles = driver.getWindowHandles();
  System.out.println(multipleWindowHandles);
  System.out.println("Entry1");
  String newWindowKey = "";
  System.out.println("Entry2");
  for (String windowKeys : multipleWindowHandles) {
    System.out.println("1"+windowKeys);
    if(windowKeys.equals(originalWindowKey) == false){
     System.out.println("Success");
     newWindowKey = windowKeys; 
    }  
  }
  
  driver.switchTo().window(newWindowKey); 
 }

code2:

 public void sampleSwitch(){
  
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
  driver.switchTo().defaultContent();
  
 }

Code3:

 public void oneSwitch(){
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);
 }

The above following ways i used for switching the tabs, but no luck yet. Please help me in this regard.

Chetan_K
  • 57
  • 1
  • 2
  • 7
  • This is nice solution to switch windows, it seems you didnt try it: http://stackoverflow.com/a/9597714/4855333 – kotoj Aug 23 '16 at 07:54
  • Here you have another possibility http://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver-with-java – Ricardo Vila Aug 23 '16 at 07:55
  • Possible duplicate of [How to switch to the new browser window, which opens after click on the button](http://stackoverflow.com/questions/9588827/how-to-switch-to-the-new-browser-window-which-opens-after-click-on-the-button) – Ricardo Vila Aug 23 '16 at 07:56
  • @kotoj: The solution is for switching between two windows, but in my case i need to switch between 2 tabs in same window. I also tried your solution in code1 that i have mentioned but it is not working – Chetan_K Aug 23 '16 at 08:00
  • @RicardoVila: The link you have mentioned contains the code to open a new empty tab or to open an url in new tab. But in my case a new tab2 will be opened after a link click in tab1. – Chetan_K Aug 23 '16 at 08:05
  • @Chetan_K Which browser you are working with? – Grasshopper Aug 23 '16 at 08:23
  • Chetan_K: did you tried the code I linked to switch new window? New tab and new window are the same, the difference is only browser configuration – kotoj Aug 23 '16 at 08:46
  • @Grasshopper: Chrome browser – Chetan_K Aug 23 '16 at 09:39
  • @kotoj: Can you please let me know how to change the browser config to make window and tab as same. – Chetan_K Aug 23 '16 at 09:41
  • I meant that both new tab and new window are new 'instance of browser'. Window or tab are only ways to display it. I've not been working with chrome driver, in firefox there are some options in 'about:config' – kotoj Aug 23 '16 at 09:47
  • For example, in firefox, when you change attribute browser.link.open_newwindow to value 2, all links will open in new window instead of new tab. – kotoj Aug 23 '16 at 09:57

2 Answers2

0

You can avoid this issue with little hack by changing target attribute with JavaScript:

import org.openqa.selenium.JavascriptExecutor;

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector('a[href="http://store.demoqa.com"]').target='_self';");

This should force webdriver to open new URL in same tab

Andersson
  • 51,635
  • 17
  • 77
  • 129
0
Set<String> handles = driver.getWindowHandles();
driver.switchTo().window(handles.toArray(new String[1])[1]);

System.out.println("Handles - " + handles.size());
System.out.println("Title - " + driver.getTitle());     
System.out.println("Menu text - " + driver.findElement(By.id("menu-main-menu")).getText());



RESULT -- 
Handles - 2
Title - ONLINE STORE | Toolsqa Dummy Test site
Menu text - Home
Product Category
All Product
Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • Getting 'java.lang.ArrayIndexOutOfBoundsException: 1' error if i use the above code. – Chetan_K Aug 24 '16 at 04:27
  • It works perfectly for me and I am running on windows 8 with latest chrome and chromedriver version. If the index exception is coming from switchto line then use this instead - driver.switchTo().window(handles.toArray(new String[handles.size()])[1]); – Grasshopper Aug 24 '16 at 05:19