gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
用python自动登陆时,出现错误selenium.common.exceptions.NoSuchWindowException: Message: no such…
- User
用python自动登陆时,出现错误selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
- Assistant
这个错误通常是因为窗口已经被关闭,而你还在尝试使用它。你可以在获取窗口句柄之前使用time.sleep()函数暂停一段时间,或者尝试使用WebDriverWait().until()方法等待窗口的可见性或存在性。
例如:
pythonimport time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 等待3秒钟 time.sleep(3) # 窗口句柄 window_handle = driver.window_handles[-1] # 等待窗口可见 WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='your-window-class']"))) # 切换窗口 driver.switch_to.window(window_handle)这样就可以避免出现NoSuchWindowException的错误。