自动化测试之alert弹窗的切换
進入頁面,定位元素,成功
hello_el=driver.find_element_by_path(’//p[@id=“hello”]’)
點擊
hello_el.click()
此時出現彈窗
再次定位頁面元素,失敗,
hello_el=driver.find_element_by_path(’//p[@id=“hello”]’)
原因:因為點擊之后會出現彈窗,彈窗中沒有該元素,所以會定位失敗。
怎么辦呢? 關閉彈窗
關閉彈窗操作:
1、先切換到彈窗
坑 ,alert是屬性,沒有括號,一般不需要等待,如果需要等待,用顯性等待
alert=driver.switch_to.alert
2、確認或者取消操作
alert.accept()或alert.dismiss()
3、再次定位元素,成功
hello_el=driver.find_element_by_xpath(’//p[@id=“hello”]’)
顯性等待-------一般不會用,彈窗點擊一下立即會出現,不會加載很長時間
alert=WebDriverWait(driver,20).until(expected_conditions.alert_is_present)
alert.accept()
定位元素,成功
hello_el=driver.find_element_by_xpath(’//p[@id=“hello”]’)
總結
以上是生活随笔為你收集整理的自动化测试之alert弹窗的切换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动化测试之iframe窗口的切换
- 下一篇: 自动化测试之鼠标悬浮操作、双击、鼠标拖拽