1.檢查是不是元素可變(如動態id)
2.元素定位方式是不是有錯誤
3.頁面重新渲染,變成舊的元素,再點擊肯定會失敗
常見場景:1)循環點擊刪除按鈕、2)點擊頁面按鈕,頁面重新渲染(比如AngularJs)
4.頁面嵌套了iframe
#參數可以是id屬性、name、直接定位到iframe的webElement。這里g_frame是id屬性
driver.switch_to.frame('g_frame')
延伸:
切換到主HTML:
driver.switch_to.default_content()
返回上一層frame:
driver.switch_to.parent_frame()
5.頁面可能切換到新的窗口
沒有關注到頁面跳轉到新窗口了,定位元素,弄了差不多1小時吧,我脆弱的小心臟啊。。。。。。。。
這里容易出現的問題:NoSuchWindowException: Message: no such window: window was already closed
或者NoSuchElementException: Message: no such element: Unable to locate element:
#切換webview到新的窗口
# 保存主窗口的handle
mainwindow=self.driver.current_window_handle # 打開一個新的web頁面
for one in driver.window_handles: driver.switch_to.window(one) if '下發學習任務' in self.driver.title: break
#進行一些操作后 。。。。
#回到主窗口
driver.switch_to.window(mainwindow)
雖然切換到【下發學習任務】窗口操作后,窗口會自動關閉。沒有切換回主窗口,還是會報錯NoSuchWindowException: Message: no such window: window was already closed
解決方法:driver.switch_to.window(mainwindow)
6.頁面加載太慢,還沒有加載出頁面,就開始找元素