網站首頁 編程語言 正文
前言
在Selenium自動化測試過程中會遇到定位瀏覽器彈窗的情況,根據彈窗實現原理不同大致可分為以下幾種定位方式。
1.?alert、confirm、prompt類型彈框
這三種彈框是JavaScript核心對象window對象中的三種方法。
1.1 alert彈框
警告消息框 alert 方法有一個參數,即希望對用戶顯示的文本字符串。該字符串不是 HTML 格式。該消息框提供了一個“確定”按鈕讓用戶關閉該消息框,并且該消息框是模式對話框,也就是說,用戶必須先關閉該消息框然后才能繼續進行操作。
selenium處理alert() 提示框:
- driver.switchTo().alert(); 獲取alert
- alert.accept(); 點確定
- alert.dismiss(); 點取消
- alert.getText();獲取alert的內容
alert彈框定位代碼:
try{
Alert alert =driver.switchTo().alert(); //使用driver.switchTo().alert()方法獲取到alert對象
Assert.assertEquals("彈框實際文本", alert.getText()); //斷言彈框文本是否和預期一致
alert.accept(); //點擊確定
// alert.dismiss(); //點擊取消
}catch(NoAlertPresentException exception){ //彈框未顯示,則跑出異常
Assert.fail("嘗試操作的alert框沒有被找到");
exception.printStackTrace();
}
或官網推薦方法
# Click the link to activate the alert
driver.find_element(By.LINK_TEXT, "See an example alert").click()
# Wait for the alert to be displayed and store it in a variable
alert = wait.until(expected_conditions.alert_is_present())
# Store the alert text in a variable
text = alert.text
# Press the OK button
alert.accept()
1.2?confirm彈框
確認消息框 使用確認消息框可向用戶問一個“是-或-否”問題,并且用戶可以選擇單擊“確定”按鈕或者單擊“取消”按鈕。confirm 方法的返回值為 true 或 false。該消息框也是模式對話框:用戶必須在響應該對話框(單擊一個按鈕)將其關閉后,才能進行下一步操作。
selenium處理confirm() 提示框:
同alert一致
try{
Alert alert =driver.switchTo().alert();
Assert.assertEquals("彈框實際文本", alert.getText());
alert.accept();
// alert.dismiss();
}catch(NoAlertPresentException exception){
Assert.fail("嘗試操作的alert框沒有被找到");
exception.printStackTrace();
}
或官網推薦方法
# Click the link to activate the alert
driver.find_element(By.LINK_TEXT, "See a sample confirm").click()
# Wait for the alert to be displayed
wait.until(expected_conditions.alert_is_present())
# Store the alert in a variable for reuse
alert = driver.switch_to.alert
# Store the alert text in a variable
text = alert.text
# Press the Cancel button
alert.dismiss()
1.3?prompt彈框
提示消息框 提供了一個文本字段,用戶可以在此字段輸入一個答案來響應您的提示。該消息框有一個“確定”按鈕和一個“取消”按鈕。如果您提供了一個輔助字符串參數,則提示消息框將在文本字段顯示該輔助字符串作為默認響應。否則,默認文本為 "<undefined>"。
selenium處理prompt() 提示框:
try{
Alert alert =driver.switchTo().alert();
Assert.assertEquals("彈框實際文本", alert.getText());
alert.sendKeys("promt框中輸入的內容");
alert.accept();
// alert.dismiss();
}catch(NoAlertPresentException exception){
Assert.fail("嘗試操作的alert框沒有被找到");
exception.printStackTrace();
}
或官網推薦方法
# Click the link to activate the alert
driver.find_element(By.LINK_TEXT, "See a sample prompt").click()
# Wait for the alert to be displayed
wait.until(expected_conditions.alert_is_present())
# Store the alert in a variable for reuse
alert = Alert(driver)
# Type your message
alert.send_keys("Selenium")
# Press the OK button
alert.accept()
2. div彈框
div彈窗是瀏覽器中比較好定位的彈窗,定位的方法與普通的元素一樣。不過這里會有一個坑,明明可以找到這個按鈕,但是就是定位不到。這個就是因為當前有div彈窗彈出的時候,需要設置一下等待時間,等頁面元素加載完畢,再去做其他操作。這里用百度登陸為例子:
3. 新標簽頁彈窗
新標簽頁彈窗,則需要進行窗口的切換。
彈出內容是嵌入的窗口解決思路:
# 打印所有的handle
all_handles = driver.window_handles
? ? print(all_handles)
# 切換到新的handle上
driver.switch_to.window(all_handles[N])
其中,獲取的句柄下標從0開始,即第一個窗口為[0]、第二個窗口為[1],如此類推。使用switch_to.window方法切換到新標簽頁后就可以做其他操作了。
此處第一個窗口打開百度首頁,在打開一個新窗口打開京東首頁,在兩個窗口之間進行切換。切換到不同的窗口之后,就可以用常規的方法進行元素的定位。
4. 彈出框是iframe
driver.switch_to.frame("frame1")之后進行定位元素
具體定位方式查看我的另一篇博客:Selenium之定位及切換frame(iframe)
參考文章:
1.關于Python+selenium 定位瀏覽器彈窗元素
2.selenium定位彈框元素
3.https://www.jb51.net/article/156978.htm
4.selenium多個瀏覽器窗口
總結
原文鏈接:https://blog.csdn.net/lovedingd/article/details/111089391
相關推薦
- 2022-07-21 vscode代碼保存時自動格式化
- 2022-05-29 .NET?Core中的HttpClientFactory類用法詳解_實用技巧
- 2022-08-13 Redis - 數據結構和持久化機制
- 2022-09-27 Android?JetPack組件的支持庫Databinding詳解_Android
- 2022-11-30 關于頁面加載即執行JQuery的三種方法小結_jquery
- 2022-07-26 Python使用psutil獲取系統信息_python
- 2022-04-30 Python代碼顯得Pythonic(區別于其他語言的寫法)_python
- 2022-06-26 django中模板繼承與ModelForm實例詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支