[英]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: 因此,您必须:
number_of_windows_to_be(2)
number_of_windows_to_be(2)
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.