如何使用Firefox驱动程序处理Python Selenium中的多个窗口

如何使用Firefox驱动程序处理Python Selenium中的多个窗口

问题描述:

我想学习Python Selenium中的 WINDOW 处理.

I want to learn WINDOW handling in Python Selenium.

我的任务是:

首先打开"Google.com".

First open 'Google.com'.

新窗口中第二次打开"Yahoo.com".

Second open 'Yahoo.com' in New Window.

第三次切换回第一个窗口,然后点击Gmail链接.

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

第四次切换到第二个窗口,然后点击Finance Link.

Fourth switch to Second Window and click on Finance Link.

以下代码对我有用:

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:

首先打开"Google.com".

First open 'Google.com'.

新窗口中第二次打开"Yahoo.com".

Second open 'Yahoo.com' in New Window.

第三保留在同一窗口中,然后单击Finance Link.

Third remaining in same window and click on Finance Link.

第四次切换到第一个窗口,然后点击Gmail链接.

Fourth switch to First Window and click on Gmail Link.

以下更改的任务代码,其中在新窗口中打开yahoo.com,然后单击财务链接,然后切换到包含Google.com的主窗口,然后单击Gmail链接后,工作:

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()

但是,如果我在切换到 Yahoo 标签后刷新页面,则该页面仅在 Chrome 驱动程序中有效,而在 Firefox 驱动程序.

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()

根据您更新的问题,对 Tab/窗口切换/处理

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

  • 始终跟踪父窗口句柄,以便您可以遍历其余用例.
  • 始终将具有预期条件 WebDriverWait 用作
  • 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
    

  • 浏览器快照:

    现在您已经有了两个 Window Handles ,因此您可以轻松切换到任何 TAB 来执行任何操作.

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