selenuim驱动IE浏览器报错:selenium.common.exceptions.NoSuchWindowException: Message: Currently focused window has been closed.
- selenuim驱动IE浏览器报错:selenium.common.exceptions.NoSuchWindowException: Message: Currently focused window has been closed.
-
selenuim驱动IE浏览器报错:selenium.common.exceptions.NoSuchWindowException: Message: Currently focused window has been closed.
selenium.common.exceptions.NoSuchWindowException: Message:
Currently focused window has been closed.
D:Anacondaseleniumwebdrivererrorhandler.py:242: NoSuchWindowException
问题原因&解决:
电脑无法在页面自动跳转之后,获取当前页面。
window handle是惰性的,不会自动切换,如果打开了一个新的窗口,想在新窗口上获取某一元素,
需要先手动切换window handle,driver.switch_to.window(xxx_handle)
另,用driver.window_handles可以获取所有窗口句柄
解决:
在跳转页面之后、定位元素之前添加:
handles = driver.window_handles
driver.switch_to.window(handles[-1])
time.sleep(5)
定位到当前窗口句柄,再进行元素定位。窗口句柄是浏览器拥有的,元素没有窗口句柄
注意
如果元素定位失败,要检查一下是不是打开了新的窗口,如果是,则需要切换window handle,
因为它不会自动切换。