简体   繁体   English

如何使用Selenium和Python切换窗口句柄

[英]How to switch window handles using Selenium and Python

If you click a link in a Windows program other than a web browser, a pop-up window appears. 如果您单击Windows程序(而不是Web浏览器)中的链接,则会出现一个弹出窗口。 I want to get the url of this popup window. 我想获取此弹出窗口的网址。 Pop-up windows will only open in IE. 弹出窗口仅在IE中打开。

driver = webdriver.Ie('C://Users/aaa/IEDriverServer.exe')
driver.implicitly_wait(3)
pyautogui.moveTo(1576, 660)
pyautogui.click()
time.sleep(3)
driver.switch_to_window(driver.window_handles[1])
   # error =>driver.switch_to_window(driver.window_handles[1])
   #         IndexError: list index out of range
driver.get_window_position(driver.window_handles[1])

windows = driver.window_handles
   # Commenting out the above two lines will result in only one active web 
   # browser in windows.
print(windows)

driver = webdriver.Ie('C://Users/seula/IEDriverServer.exe')
driver.implicitly_wait(3)
pyautogui.moveTo(1576, 660)
pyautogui.click()
time.sleep(3)
driver.switch_to_window(driver.window_handles[1])
   # error =>driver.switch_to_window(driver.window_handles[1])
   #         IndexError: list index out of range
driver.get_window_position(driver.window_handles[1])

windows = driver.window_handles
   # Commenting out the above two lines will result in only one active web 
   # browser in windows.
print(windows)

In this source, running IEDriver.exe opens a localhost window and pops up when you click on a link to a Windows program with pyautogui. 在此源代码中,运行IEDriver.exe将打开一个localhost窗口,并在单击带有pyautogui的Windows程序的链接时弹出。 However, if I check with driver.window_handles, only the localhost window is shown and the popup window is not recognized. 但是,如果我使用driver.window_handles进行检查,则仅显示localhost窗口,而无法识别弹出窗口。 How can I get the popup window url? 如何获取弹出窗口的网址?

For get current url, you can use : 要获取当前网址,可以使用:

windows = driver.current_url
print(windows)

As Simon clearly mentioned in a discussion: 正如Simon在讨论中明确提到的那样:

While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. 虽然用于存储句柄列表的数据类型可以通过插入来排序,但是WebDriver实现在窗口句柄上迭代以插入它们的顺序并不需要稳定。 The ordering is arbitrary. 顺序是任意的。

So you have to: 因此,您必须:

  • Induce WebDriverWait for number_of_windows_to_be(2) 诱导WebDriverWait等待number_of_windows_to_be(2)
  • As the order of the windows are random, you can obtain the set of window handles before the interaction is performed and compare it with the set after the action is performed. 由于窗口的顺序是随机的,因此您可以在执行交互之前获取窗口句柄的集合,并将其与执行操作后的窗口句柄进行比较。
  • You can use the following solution: 您可以使用以下解决方案:

     driver = webdriver.Ie('C://Users/aaa/IEDriverServer.exe') driver.implicitly_wait(3) windows_before = browser.current_window_handle pyautogui.moveTo(1576, 660) pyautogui.click() 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) 

References: You can find a couple of relevant discussions in: 参考资料:您可以在以下位置找到一些相关的讨论:

I usually use send_keys() instead of click() to handle pop-up window. 我通常使用send_keys()而不是click()来处理弹出窗口。

Try to use following code: 尝试使用以下代码:

pyautogui.send_keys(Keys.CONTROL + Keys.ENTER)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + "2")
# window_handles[-1] refer to last window created.
driver.switch_to.window(self.driver.window_handles[-1])
url = driver.current_url
print(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 窗口句柄未在Python Selenium上更新 - window handles not updating on Python Selenium Python selenium - 如何切换到下一个 window - Python selenium - How to switch to next window 如何在 Selenium for Python 中切换到新窗口? - How to switch to new window in Selenium for Python? 硒+ python switch_to_window - selenium + python switch_to_window 如何使用预期条件 new_window_is_opened 切换选项卡并使用 Selenium 和 Python 验证页面标题 - How to switch tabs using the expected conditions new_window_is_opened and verify the page title using Selenium and Python 如何使用Python获取Ubuntu Terminal的窗口句柄? - How to get Ubuntu Terminal's window handles using Python? 如何在Python中使用Selenium在由不同WebDriver打开的不同Chrome浏览器窗口之间切换? - How to switch between different chrome browser window opened by different WebDriver using selenium in Python? 如何使用 Selenium 和 Python 在 iframe 之间切换? - How to switch between iframes using Selenium and Python? 如何获取 python selenium 中每个 window_handles 项目的标题 - How can I get the titles of each window_handles item in python selenium 如何使用 Selenium 在 Python 中切换到模式 - How to switch to a modal in Python using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM