1

I wrote sample code but it is not working. Also observed that there is only 1 window handle for 2 tabs. How to switch to parent tab again?

 driver = webdriver.Firefox()
 driver.set_page_load_timeout(60)
 driver.implicitly_wait(15)
 driver.get("https://www.google.co.in")
 oldtab = driver.current_window_handle
 print oldtab
 print driver.title
 body = driver.find_element_by_tag_name("body")
 print 'new tab opened'
 driver.get("http://gmail.com/")
 print driver.title
 print 'back to old tab'
 driver.switch_to_window(oldtab)
 print driver.title
 for handle in driver.window_handles:
    print "Handle = ",handle
Venu
  • 353
  • 3
  • 6
  • 18

5 Answers5

5

You need to switch tab using Keys before switching handle to parent tab.

 from selenium.webdriver.common.keys import Keys

 driver = webdriver.Firefox()
 driver.set_page_load_timeout(60)
 driver.implicitly_wait(15)

 # First Tab
 driver.get("https://www.google.co.in")
 oldtab = driver.current_window_handle
 print driver.title
 time.sleep(3)

 # Second Tab
 driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t")
 driver.get("http://gmail.com/")
 newtab = driver.current_window_handle
 print driver.title
 time.sleep(3)

 # Go back to First Tab
 driver.find_element_by_tag_name("body").send_keys(Keys.ALT + Keys.NUMPAD1)
 driver.switch_to_window(oldtab)
 print driver.title
 time.sleep(3)

 # Go to Second Tab again
 driver.find_element_by_tag_name("body").send_keys(Keys.ALT + Keys.NUMPAD2)
 driver.switch_to_window(newtab)
 print driver.title
 time.sleep(3)
Dhiraj
  • 481
  • 6
  • 13
1

Another way you can achieve this is - open two instances of browser say driver1 and driver2 and open respective url in a browser instance and perform actions on it -

driver1 = webdriver.Firefox()
driver1.get("https://www.google.co.in")
//perform actions for page https://www.google.co.in


driver2 = webdriver.Firefox()
driver2.get("http://gmail.com/")
//perform actions for page http://gmail.com/
Alpha
  • 13,320
  • 27
  • 96
  • 163
0

Following solution is working for me.

ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
time.sleep(5)    
ActionChains(driver).key_down(Keys.CONTROL).send_keys(Keys.NUMPAD1).key_up(Keys.CONTROL).perform()
Venu
  • 353
  • 3
  • 6
  • 18
0

Another complete version (no snippet) in windows (Firefox)

Edited Dhiraj's code to make it working in FF41 in windows

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.set_page_load_timeout(60)
driver.implicitly_wait(15)

# First Tab
driver.get("https://www.google.co.in")
oldtab = driver.current_window_handle
print driver.title
time.sleep(3)

# Second Tab
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t")
driver.get("http://gmail.com/")
newtab = driver.current_window_handle
print driver.title
time.sleep(3)

# Go back to First Tab
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP)
driver.switch_to_window(oldtab)
print driver.title
time.sleep(3)

# Go to Second Tab again
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP)
driver.switch_to_window(newtab)
print driver.title
time.sleep(3)

driver.close()

It prints

Google
Gmail
Google
Gmail
Learner
  • 5,192
  • 1
  • 24
  • 36
0
String str1 = driver.findElement(By.xpath("//*[@class='lft']//div[@class='expColMenu']["+i+"]//div[2]/div[1]/a")).getAttribute("href");
System.out.println(str1);
Thread.sleep(1000);
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));      
driver.get(str1);
Thread.sleep(1000);
System.out.println("Title  =  "+driver.getTitle());
driver.close();
driver.switchTo().window(tabs.get(0));System.out.println();