Toggle tabs using Selenium WebDriver with Java

Using Selenium WebDriver with JAVA, I am trying to automate functionality where I need to open a new tab, perform some operations there and return to the previous tab (Parent). I used the shift knob but didn't work. And one strange thing is that two tabs have the same window handle, because of which I can not switch between tabs.

However, when I try to use different Firefox windows, it works, but for the tab it does not work.

Please help me how can I switch to tabs. OR how can I switch tabs without using a window handle, since the window handle matches both tabs in my case.

(I noticed that when you open different tabs in the same window, the window handle remains the same.)

+41
java selenium testing automation selenium-webdriver
04 Oct
source share
15 answers
psdbComponent.clickDocumentLink(); ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles()); driver.switchTo().window(tabs2.get(1)); driver.close(); driver.switchTo().window(tabs2.get(0)); 

This code worked fine for me. Give it a try. You always need to switch the driver to a new tab before you want to do something on a new tab.

+62
Apr 08 '14 at 5:31 on
source share

This is a simple solution to open a new tab, change focus on it, close the tab and return focus to the old / original tab:

 @Test public void testTabs() { driver.get("https://business.twitter.com/start-advertising"); assertStartAdvertising(); // considering that there is only one tab opened in that point. String oldTab = driver.getWindowHandle(); driver.findElement(By.linkText("Twitter Advertising Blog")).click(); ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles()); newTab.remove(oldTab); // change focus to new tab driver.switchTo().window(newTab.get(0)); assertAdvertisingBlog(); // Do what you want here, you are in the new tab driver.close(); // change focus back to old tab driver.switchTo().window(oldTab); assertStartAdvertising(); // Do what you want here, you are in the old tab } private void assertStartAdvertising() { assertEquals("Start Advertising | Twitter for Business", driver.getTitle()); } private void assertAdvertisingBlog() { assertEquals("Twitter Advertising", driver.getTitle()); } 
+16
May 14 '13 at 11:27
source share

There is a difference in how the web driver handles different windows and how it handles different tabs.

Case 1:
If there are multiple windows, the following code may help:

 //Get the current window handle String windowHandle = driver.getWindowHandle(); //Get the list of window handles ArrayList tabs = new ArrayList (driver.getWindowHandles()); System.out.println(tabs.size()); //Use the list of window handles to switch between windows driver.switchTo().window(tabs.get(0)); //Switch back to original window driver.switchTo().window(mainWindowHandle); 


Case 2:
If there are several tabs in one window, then there is only one window handle. Therefore, switching between windows processes the control in one tab.
In this case, using Ctrl + \ t (Ctrl + Tab) to switch between tabs is more useful.

 //Open a new tab using Ctrl + t driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); //Switch between tabs using Ctrl + \t driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t"); 

A detailed code example can be found here:
http://design-interviews.blogspot.com/2014/11/switching-between-tabs-in-same-browser-window.html

+9
Nov 27 '14 at 15:06
source share

Work

Assumption: clicking something on your webpage will open a new tab.

Use the following logic to go to the second tab.

 new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform(); 

In the same way, you can return to the first tab again.

 new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD1).build().perform(); 
+6
04 Oct
source share
 String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN); WebElement e = driver.findElement(By .xpath("html/body/header/div/div[1]/nav/a")); e.sendKeys(selectLinkOpeninNewTab);//to open the link in a current page in to the browsers new tab e.sendKeys(Keys.CONTROL + "\t");//to move focus to next tab in same browser try { Thread.sleep(8000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //to wait some time in that tab e.sendKeys(Keys.CONTROL + "\t");//to switch the focus to old tab again 

Hope this helps you.

+3
Apr 24 '15 at 7:11
source share

The first thing you need to do is open a new tab and save its handle name. This is best done using javascript rather than keys (ctrl + t), since keys are not always available on automation servers. Example:

 public static String openNewTab(String url) { executeJavaScript("window.parent = window.open('parent');"); ArrayList<String> tabs = new ArrayList<String>(bot.driver.getWindowHandles()); String handleName = tabs.get(1); bot.driver.switchTo().window(handleName); System.setProperty("current.window.handle", handleName); bot.driver.get(url); return handleName; } 

The second thing you need to do is switch between tabs. Doing this only with the buttons on the switch window will not always work, since the tab you are working on will not always be in focus, and Selenium will refuse from time to time. As I said, using keys is a little problematic, and javascript doesn't really support tab switching, so I used warnings to switch tabs, and it worked like a charm:

 public static void switchTab(int tabNumber, String handleName) { driver.switchTo().window(handleName); System.setProperty("current.window.handle", handleName); if (tabNumber==1) executeJavaScript("alert(\"alert\");"); else executeJavaScript("parent.alert(\"alert\");"); bot.wait(1000); driver.switchTo().alert().accept(); } 
+2
Oct 19 '16 at 12:55
source share
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL,Keys.SHIFT,Keys.TAB); 

This method helps when switching between multiple windows. The limiting problem with this method is that it can only be used as many times until the desired window is reached. Hope this helps.

+1
Apr 28 '16 at 17:17
source share

With Selenium 2.53.1 using firefox 47.0.1 as a WebDriver in Java: no matter how many tabs I open, "driver.getWindowHandles ()" will only return one handle, so it was not possible to switch between tabs.

As soon as I started using Chrome 51.0, I could get all the pens. The following code shows how to access multiple drivers and multiple tabs in each driver.

 // INITIALIZE TWO DRIVERS (THESE REPRESENT SEPARATE CHROME WINDOWS) driver1 = new ChromeDriver(); driver2 = new ChromeDriver(); // LOOP TO OPEN AS MANY TABS AS YOU WISH for(int i = 0; i < TAB_NUMBER; i++) { driver1.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB Thread.sleep(100); // STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS ArrayList tabs1 = new ArrayList<String> (driver1.getWindowHandles()); // REPEAT FOR THE SECOND DRIVER (SECOND CHROME BROWSER WINDOW) // LOOP TO OPEN AS MANY TABS AS YOU WISH for(int i = 0; i < TAB_NUMBER; i++) { driver2.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB Thread.sleep(100); // STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS ArrayList tabs2 = new ArrayList<String> (driver1.getWindowHandles()); // NOW PERFORM DESIRED TASKS WITH FIRST BROWSER IN ANY TAB for(int ii = 0; ii <= TAB_NUMBER; ii++) { driver1.switchTo().window(tabs1.get(ii)); // LOGIC FOR THAT DRIVER CURRENT TAB } // PERFORM DESIRED TASKS WITH SECOND BROWSER IN ANY TAB for(int ii = 0; ii <= TAB_NUMBER; ii++) { drvier2.switchTo().window(tabs2.get(ii)); // LOGIC FOR THAT DRIVER CURRENT TAB } 

Hope this gives you an idea of ​​how to manage multiple tabs in multiple browser windows.

+1
Aug 24 '16 at
source share

I had a problem recently, the link was opened on a new tab, but selenium was still on the initial tab.

I use Chromedriver, and the only way to focus on the tab is to use switch_to_window() .

Here's the Python code:

 driver.switch_to_window(driver.window_handles[-1]) 

So, the tip should find out the name of the required window handle, it is stored as a list in

 driver.window_handles 
0
Apr 29 '13 at 5:00
source share

See below:

 WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.irctc.co.in/"); String oldTab = driver.getWindowHandle(); //For opening window in New Tab String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); driver.findElement(By.linkText("Hotels & Lounge")).sendKeys(selectLinkOpeninNewTab); // Perform Ctrl + Tab to focus on new Tab window new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, Keys.TAB)).perform(); // Switch driver control to focused tab window driver.switchTo().window(oldTab); driver.findElement(By.id("textfield")).sendKeys("bangalore"); 

Hope this will be helpful!

0
Jan 31 '14 at 11:39
source share

This is a very simple process: suppose you have two tabs, so you need to close the current tab first using client.window(callback) , because the switch command "switches to the first available". Then you can easily switch to the tab using client.switchTab .

0
Apr 14 '15 at 6:17
source share

A brief example of switching between tabs in a browser (in the case of a single window):

 // open the first tab driver.get("https://www.google.com"); Thread.sleep(2000); // open the second tab driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); driver.get("https://www.google.com"); Thread.sleep(2000); // switch to the previous tab driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "" + Keys.SHIFT + "" + Keys.TAB); Thread.sleep(2000); 

I write Thread.sleep(2000) just to have a timeout to see switching between tabs.

You can use CTRL + TAB to go to the next tab and CTRL + SHIFT + TAB to go to the previous tab.

0
Apr 22 '16 at 13:09 on
source share

This will work for MacOS for Firefox and Chrome:

 // opens the default browser tab with the first webpage driver.get("the url 1"); thread.sleep(2000); // opens the second tab driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t"); driver.get("the url 2"); Thread.sleep(2000); // comes back to the first tab driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{"); 
0
Sep 08 '16 at 10:12
source share

To get the handle of the parent window.

 String parentHandle = driverObj.getWindowHandle(); public String switchTab(String parentHandle){ String currentHandle =""; Set<String> win = ts.getDriver().getWindowHandles(); Iterator<String> it = win.iterator(); if(win.size() > 1){ while(it.hasNext()){ String handle = it.next(); if (!handle.equalsIgnoreCase(parentHandle)){ ts.getDriver().switchTo().window(handle); currentHandle = handle; } } } else{ System.out.println("Unable to switch"); } return currentHandle; } 
0
Aug 09 '17 at 6:36 on
source share

A simple answer that worked for me:

 for (String handle1 : driver1.getWindowHandles()) { System.out.println(handle1); driver1.switchTo().window(handle1); } 
0
Oct 09 '17 at 18:31 on
source share



All Articles