Abhay
Abhay

Reputation: 526

How to handle multiple windows in Python selenium with Firefox driver

I want to learn WINDOW handling in Python Selenium.

My Task is:

First open 'Google.com'.

Second open 'Yahoo.com' in New Window.

Third switch back to First Window and click on Gmail Link.

Fourth switch to Second Window and click on Finance Link.

Following Code works for me:

browser.get("http://www.google.co.in")
browser.execute_script("window.open('https://www.yahoo.com')")
browser.switch_to_window(browser.window_handles[0])
print(browser.title)
gmail=browser.find_element_by_class_name("gb_P")
gmail.click()
browser.switch_to_window(browser.window_handles[1])
print(browser.title)
fin=browser.find_element_by_link_text("Finance")
fin.click()

But when I try to change sequence to task as:

First open 'Google.com'.

Second open 'Yahoo.com' in New Window.

Third remaining in same window and click on Finance Link.

Fourth switch to First Window and click on Gmail Link.

The below altered code for the task in which after opening yahoo.com in new window and then clicking on finance link and then switching to main window containing Google.com then clicking on Gmail link doesn't work:

browser.get("http://www.google.co.in")
browser.execute_script("window.open('https://www.yahoo.com')")
browser.switch_to_window(browser.window_handles[1])
print(browser.title)
fin=browser.find_element_by_link_text("Finance")
fin.click()
browser.switch_to_window(browser.window_handles[0])
print(browser.title)
gmail=browser.find_element_by_class_name("gb_P")
gmail.click()

But if I refresh the page after switching to the Yahoo tab this works only in Chrome Driver and not in Firefox Driver.

browser.get("http://www.google.co.in")
print(browser.current_window_handle)
browser.execute_script("window.open('https://www.yahoo.com')")
print(browser.current_window_handle)

WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))


browser.switch_to_window(browser.window_handles[1])
print(browser.current_window_handle)
print(browser.title)

browser.refresh()

fin=browser.find_element_by_link_text("Finance")
fin.click()
print(browser.window_handles)

WebDriverWait(browser,10000)

browser.switch_to_window(browser.window_handles[0])
print(browser.title)
print(browser.current_window_handle)
gmail=browser.find_element_by_class_name("gb_P")
gmail.click()

Upvotes: 2

Views: 8912

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193308

As per your updated question a few words about Tab/Window switching/handling:

  • Always keep track of the Parent Window handle so you can traverse back for the rest of your usecases.
  • Always use WebDriverWait with expected-conditions as number_of_windows_to_be(num_windows)
  • Always keep track of the Child Window handles so you can traverse back if required.
  • Here is your own code with some minor tweaks mentioned above:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    #other lines of code
    browser.get("http://www.google.co.in")
    print("Initial Page Title is : %s" %browser.title)
    windows_before  = browser.current_window_handle
    print("First Window Handle is : %s" %windows_before)
    browser.execute_script("window.open('https://www.yahoo.com')")
    WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
    windows_after = browser.window_handles
    new_window = [x for x in windows_after if x != windows_before][0]
    # browser.switch_to_window(new_window) <!---deprecated>
    browser.switch_to.window(new_window)
    print("Page Title after Tab Switching is : %s" %browser.title)
    print("Second Window Handle is : %s" %new_window)
    
  • Console Output:

    Initial Page Title is : Google
    First Window Handle is : CDwindow-34D74AB1BB2F0A1A8B7426BF25B86F52
    Page Title after Tab Switching is : Yahoo
    Second Window Handle is : CDwindow-F3ABFEBE4907CFBB3CD09CEB75ED570E
    

Browser Snapshot:

TAB_SWITCH

Now you have got both the Window Handles so you can easily switch to any of the TABs to perform any action.

Upvotes: 1

Related Questions