Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

python - Selenium Switch Tabs

Since Firefox does not support Control + T anymore for the tab, I started using driver.execute_script("window.open('URL', 'new_window')")

I am trying to display the title of the different tab I open and switch between them. For the example below, I expect the output to be facebook, google and back to facebook. Right now the output is facebook, facebook and facebook.

I tried the answer from here but it also did not work: Switch back to parent tab using selenium webdriver

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.facebook.com/")
print(driver.title)

driver.execute_script("window.open('http://google.com', 'new_window')")
print(driver.title)

driver.switch_to.window(driver.window_handles[0])
print(driver.title)

UPDATED: I tried the follow code and it still did not work.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.facebook.com/")
print(driver.title)
window_before = driver.window_handles[0]

driver.execute_script("window.open('http://google.com', 'new_window')")
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
print(driver.title)
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A few words about Tab/Window switching/handling:

  • Always keep track of the Parent Window handle so you can traverse back later if required as per your usecase.

  • Always use WebDriverWait with expected_conditions as number_of_windows_to_be(num_windows) before switching between Tabs/Windows.

  • Always keep track of the Child Window handles so you can traverse whenever required.

  • Always use WebDriverWait with expected_conditions as title_contains("partial_page_title") before extracting the Page Title.

  • 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
    
    driver = webdriver.Firefox(executable_path=r'C:WebDriversgeckodriver.exe')
    driver.get("http://www.facebook.com/")
    print("Initial Page Title is: %s" %driver.title)
    windows_before  = driver.current_window_handle
    driver.execute_script("window.open('http://google.com')")
    WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
    windows_after = driver.window_handles
    new_window = [x for x in windows_after if x != windows_before][0]
    driver.switch_to.window(new_window)
    WebDriverWait(driver, 20).until(EC.title_contains("G"))
    print("Page Title after first window switching is: %s" %driver.title)
    driver.close()
    driver.switch_to.window(windows_before)
    WebDriverWait(driver, 20).until(EC.title_contains("F"))
    print("Page Title after second window switching is: %s" %driver.title)
    driver.quit()
    
  • Console Output:

    Initial Page Title is: Facebook – log in or sign up
    Page Title after first window switching is: Google
    Page Title after second window switching is: Facebook – log in or sign up
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...