日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Selenium定位瀏覽器彈窗方法實例總結_python

作者:慕城南風 ? 更新時間: 2022-08-14 編程語言

前言

在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

欄目分類
最近更新