欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Selenium定位瀏覽器彈窗方法實例總結(jié)

 更新時間:2022年06月22日 09:29:35   作者:慕城南風  
彈出框是自動化測試中一種常見的元素,這種元素通常是客戶端自帶的,下面這篇文章主要給大家介紹了關(guān)于Selenium定位瀏覽器彈窗方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

前言

在Selenium自動化測試過程中會遇到定位瀏覽器彈窗的情況,根據(jù)彈窗實現(xiàn)原理不同大致可分為以下幾種定位方式。

1. alert、confirm、prompt類型彈框

這三種彈框是JavaScript核心對象window對象中的三種方法。

1.1 alert彈框

警告消息框 alert 方法有一個參數(shù),即希望對用戶顯示的文本字符串。該字符串不是 HTML 格式。該消息框提供了一個“確定”按鈕讓用戶關(guān)閉該消息框,并且該消息框是模式對話框,也就是說,用戶必須先關(guān)閉該消息框然后才能繼續(xù)進行操作。

selenium處理alert() 提示框:

  • driver.switchTo().alert(); 獲取alert
  • alert.accept(); 點確定
  • alert.dismiss(); 點取消
  • alert.getText();獲取alert的內(nèi)容

alert彈框定位代碼:

try{
      Alert alert =driver.switchTo().alert();  //使用driver.switchTo().alert()方法獲取到alert對象
      Assert.assertEquals("彈框?qū)嶋H文本", alert.getText()); //斷言彈框文本是否和預期一致
      alert.accept(); //點擊確定
      // alert.dismiss();  //點擊取消
  }catch(NoAlertPresentException exception){ //彈框未顯示,則跑出異常
      Assert.fail("嘗試操作的alert框沒有被找到");
      exception.printStackTrace();
  }

官網(wǎng)推薦方法

# 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。該消息框也是模式對話框:用戶必須在響應該對話框(單擊一個按鈕)將其關(guān)閉后,才能進行下一步操作。

selenium處理confirm() 提示框:

同alert一致

try{
      Alert alert =driver.switchTo().alert();
      Assert.assertEquals("彈框?qū)嶋H文本", alert.getText());
      alert.accept();
      // alert.dismiss();
  }catch(NoAlertPresentException exception){
      Assert.fail("嘗試操作的alert框沒有被找到");
      exception.printStackTrace();
  }

官網(wǎng)推薦方法

# 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彈框

提示消息框 提供了一個文本字段,用戶可以在此字段輸入一個答案來響應您的提示。該消息框有一個“確定”按鈕和一個“取消”按鈕。如果您提供了一個輔助字符串參數(shù),則提示消息框?qū)⒃谖谋咀侄物@示該輔助字符串作為默認響應。否則,默認文本為 "<undefined>"。

selenium處理prompt() 提示框:

try{
      Alert alert =driver.switchTo().alert();
      Assert.assertEquals("彈框?qū)嶋H文本", alert.getText());
 
      alert.sendKeys("promt框中輸入的內(nèi)容");
      alert.accept();
      // alert.dismiss();
  }catch(NoAlertPresentException exception){
      Assert.fail("嘗試操作的alert框沒有被找到");
      exception.printStackTrace();
  }

官網(wǎng)推薦方法

# 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彈窗彈出的時候,需要設(shè)置一下等待時間,等頁面元素加載完畢,再去做其他操作。這里用百度登陸為例子:

3. 新標簽頁彈窗

新標簽頁彈窗,則需要進行窗口的切換。

彈出內(nèi)容是嵌入的窗口解決思路:

# 打印所有的handle

all_handles = driver.window_handles

    print(all_handles)

# 切換到新的handle上

driver.switch_to.window(all_handles[N])

其中,獲取的句柄下標從0開始,即第一個窗口為[0]、第二個窗口為[1],如此類推。使用switch_to.window方法切換到新標簽頁后就可以做其他操作了。

此處第一個窗口打開百度首頁,在打開一個新窗口打開京東首頁,在兩個窗口之間進行切換。切換到不同的窗口之后,就可以用常規(guī)的方法進行元素的定位。

4. 彈出框是iframe

driver.switch_to.frame("frame1")之后進行定位元素

具體定位方式查看我的另一篇博客:Selenium之定位及切換frame(iframe)

參考文章:

1.關(guān)于Python+selenium 定位瀏覽器彈窗元素

2.selenium定位彈框元素

3.http://www.dbjr.com.cn/article/156978.htm

4.selenium多個瀏覽器窗口

總結(jié)

到此這篇關(guān)于Selenium定位瀏覽器彈窗方法的文章就介紹到這了,更多相關(guān)Selenium定位瀏覽器彈窗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論