Windows 处理 如果我尝试关闭 python 中的当前 window,则关闭整个浏览器
Windows Handling Closes the whole browser if i try to close the current window in python
我目前正在使用 windows 处理在新 window 中打开地图方向,打开后我将关闭子 window,它已打开并进行重制在 code.But 中工作它关闭了整个浏览器,调试时它工作正常,但是在 运行 代码中,我收到错误,
错误 - selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
下面附上代码,
##Clicking on The Map Image
self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
##Setting up an Window Handle to get the size.
handels =self.driver.window_handles
size = len(handels)
"""
The Below For Loop, We are using For Handling The Mutilple Windows,
Which are opened in the Browser.
"""
for length in range(size):
driver.switch_to.window(handels[length])
print(self.driver.title)
time.sleep(3)
if length == 1:
driver.close()
我不知道哪里犯了错误。请帮我解决一下。
尝试使用此代码关闭新 window 并切换回主 window
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Get current window
current = self.driver.current_window_handle
# Get current windows
handles = self.driver.window_handles
# Click button. Consider to use more reliable relative XPath instead of this absolute XPath
self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
# Wait for new window
WebDriverWait(self.driver, 10).until(EC.new_window_is_opened(handles))
# Switch to new window
self.driver.switch_to.window([w for w in self.driver.window_handles if w != current][0])
# Close new window
self.driver.close()
# Switch back to main window
self.driver.switch_to.window(current)
简而言之,切换到 新 window 句柄 并不干净。
解决方案
- 始终跟踪 Parent Window 句柄,以便您可以根据用例稍后返回。
- 在 Tabs/Windows.
之间切换之前始终使用 WebDriverWait with expected_conditions as number_of_windows_to_be(num_windows)
- 始终跟踪 Child Window 句柄,以便您可以在需要时遍历。
- 在提取 页面标题.[=40= 之前,始终使用 WebDriverWait 并将
expected_conditions
作为 title_contains("partial_page_title")
]
这是你自己的代码,上面提到了一些小的调整:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
handels_initially = driver.window_handles
self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
handels_now =self.driver.window_handles
new_handle = [x for x in handels_now if x != handels_initially][0]
driver.switch_to.window(new_handle)
WebDriverWait(driver, 20).until(EC.title_contains("partial_title"))
print(self.driver.title)
driver.close()
您可以在 Selenium Switch Tabs
中找到详细的讨论
我目前正在使用 windows 处理在新 window 中打开地图方向,打开后我将关闭子 window,它已打开并进行重制在 code.But 中工作它关闭了整个浏览器,调试时它工作正常,但是在 运行 代码中,我收到错误,
错误 - selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
下面附上代码,
##Clicking on The Map Image
self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
##Setting up an Window Handle to get the size.
handels =self.driver.window_handles
size = len(handels)
"""
The Below For Loop, We are using For Handling The Mutilple Windows,
Which are opened in the Browser.
"""
for length in range(size):
driver.switch_to.window(handels[length])
print(self.driver.title)
time.sleep(3)
if length == 1:
driver.close()
我不知道哪里犯了错误。请帮我解决一下。
尝试使用此代码关闭新 window 并切换回主 window
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Get current window
current = self.driver.current_window_handle
# Get current windows
handles = self.driver.window_handles
# Click button. Consider to use more reliable relative XPath instead of this absolute XPath
self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
# Wait for new window
WebDriverWait(self.driver, 10).until(EC.new_window_is_opened(handles))
# Switch to new window
self.driver.switch_to.window([w for w in self.driver.window_handles if w != current][0])
# Close new window
self.driver.close()
# Switch back to main window
self.driver.switch_to.window(current)
简而言之,切换到 新 window 句柄 并不干净。
解决方案
- 始终跟踪 Parent Window 句柄,以便您可以根据用例稍后返回。
- 在 Tabs/Windows. 之间切换之前始终使用 WebDriverWait with expected_conditions as
- 始终跟踪 Child Window 句柄,以便您可以在需要时遍历。
- 在提取 页面标题.[=40= 之前,始终使用 WebDriverWait 并将
expected_conditions
作为title_contains("partial_page_title")
] 这是你自己的代码,上面提到了一些小的调整:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC handels_initially = driver.window_handles self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click() WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)) handels_now =self.driver.window_handles new_handle = [x for x in handels_now if x != handels_initially][0] driver.switch_to.window(new_handle) WebDriverWait(driver, 20).until(EC.title_contains("partial_title")) print(self.driver.title) driver.close()
您可以在 Selenium Switch Tabs
中找到详细的讨论
number_of_windows_to_be(num_windows)