Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch tabs using Selenium WebDriver with Java

Using Selenium WebDriver with Java. I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I used switch handle but it's not working. And one strange thing the two tabs are having same window handle due to which I am not able to switch between tabs.

However when I am trying with different Firefox windows it works, but for tab it's not working.

How can I switch tabs? Or, how can I switch tabs without using window handle as window handle is same of both tabs in my case?

(I have observed that when you open different tabs in same window, window handle remains same)

like image 805
Umesh Kumar Avatar asked Oct 04 '12 14:10

Umesh Kumar


People also ask

How do you switch between tabs in Java?

In case there are multiple tabs in the same window, then there is only one window handle. Hence switching between window handles keeps the control in the same tab. In this case using Ctrl + \t (Ctrl + Tab) to switch between tabs is more useful.

How do I switch between multiple windows in Selenium?

In Selenium, when we have multiple windows in any web application, the approach may need to switch control among several windows i.e from one window to another to perform any action and we can achieve this by using switchto(); method.

Can Selenium run multiple tabs?

Just like you might open web pages in different tabs locally, it's also possible to have multiple tabs up in one browser during a Selenium test.


2 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 perfectly worked for me. Try it out. You always need to switch your driver to new tab, before you want to do something on new tab.

like image 61
Sireesha Middela Avatar answered Oct 02 '22 16:10

Sireesha Middela


This is a simple solution for opening a new tab, changing focus to it, closing 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()); } 
like image 38
Jordan Silva Avatar answered Oct 02 '22 17:10

Jordan Silva