selenium+webdriver+python 中警告框的處理方法


在自動化測試過程中,經常會遇到彈出警告框的情況,如圖所示:

這里寫代碼片

在 WebDriver 中處理 JavaScript 所生成的 alert、confirm 以及 prompt 是很簡單的。具體做法是使用 
switch_to_alert()方法定位到 alert/confirm/prompt。然后使用 text/accept/dismiss/send_keys 按需進行操做。
1. 獲取警告框的text消息 
2. 接受消息框(確定) 
3. 取消 
4. 輸入值 
text 返回 alert/confirm/prompt 中的文字信息。 
accept 點擊確認按鈕。 
dismiss 點擊取消按鈕,如果有的話。 
send_keys 輸入值,這個 alert\confirm 沒有對話框就不能用了,不然會報錯。

舉例如下: 
接受彈窗 
driver.switch_to_alert().accept() 
得到彈窗的文本消息,比如得到:請輸入用戶名! 
message=driver.switch_to_alert().text 
print(message) 
取消按鈕 
driver.switch_to_alert().dismiss() 
輸入值 
driver.switch_to_alert().send_keys(“hello”)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM