selenium webdriver處理alert、confirm、prompt

來源:互聯網
上載者:User

alert、confirm、prompt這樣的js對話方塊在selenium webdriveriver中處理十分方便簡潔。以下面html代碼為例:

 

example.html

<html>

    <head>

        <title>Alert</title>

    </head>

    <body>

        <input id = "alert" value = "alert" type ="button" onclick = "alert('你好!請按確認繼續!');"/>

        <input id = "confirm" value= "confirm" type = "button" onclick = "confirm('確定?');"/>

        <inputid = "prompt" value = "prompt" type = "button"onclick = "var name = prompt('請輸入內容:','請輸入

 內容'); document.write(name) "/>

   </body>

 </html>

 以上html代碼在頁面上顯示了三個按鈕,點擊他們分別彈出alert、confirm、prompt對話方塊。如果在prompt對話方塊中輸入文字點擊確定之後,將會重新整理頁面,顯示這些文字 。

 

selenium webdriveriver 處理這些彈層的代碼如下:

 

import org.openqa.selenium.Alert;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriveriver;

import org.openqa.selenium.firefox.FirefoxDriveriver;

 

public class Alertpop{

 

        /**

         * @author tyf

         */

        publicstatic void main(String[] args) {

               //TODO Auto-generated method stub

                WebDriveriver driver = new FirefoxDriveriver();

               Stringurl = "file:///C:/selenium/Example.html";//Example.html檔案所在的路徑

               driver.get(url);

              

               //點擊第一個按鈕,輸出對話方塊上面的文字

               driver.findElement(By.id("alert")).click();

               Alertalert = driver.switchTo().alert();

               Stringtext = alert.getText();

               System.out.println(text);

               alert.dismiss();//關閉alert

              

               //點擊第二個按鈕,輸出對話方塊上面的文字,然後確認

               driver.findElement(By.id("confirm")).click();

               Alertconfirm = driver.switchTo().alert();

               Stringtext1 = confirm.getText();

               System.out.println(text1);

               confirm.accept();

              

               //點擊第三個按鈕,輸入你的名字,然後點擊確認

               driver.findElement(By.id("prompt")).click();

               Alertprompt = driver.switchTo().alert();

               Stringtext2 = prompt.getText();

               System.out.println(text2);

               prompt.sendKeys("jarvi");

               prompt.accept();

              

        }

 

}

 

從以上代碼可以看出driver.switchTo().alert();這句可以得到alert\confirm\prompt對話方塊的對象,然後運用其方法對它進行操作。對話方塊操作的主要方法有:

 

  • getText()    得到常值內容
  • accept()      相當於點擊它的"確認"
  • dismiss()    相當於點擊"取消"對話方塊
  • sendKeys()  輸入值,這個alert\confirm沒有對話方塊就不能用了,不然會報錯。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.