Selenium对于对话框alert,confirm,prompt的处理

html 源码:


    <html>  
      
        <head>  
      
            <title>Alert</title>  
<script>
function myFunction()
 {
    var x;
    var b = prompt("hello","harry potter");
    if (b!=null && b!="") {
    
    x = "hello"+b+"欢迎你";
    
     }
    document.getElementById("display").innerHTML=x;
 }

</script>

    
        </head>  
      
        <body>  
      
            <input id = "alert" value = "alert" type = "button" onclick = "alert('欢迎!请按确认继续!');"/>  
            <input id = "confirm" value = "confirm" type = "button" onclick = "confirm('确定吗?');"/>  
            <input id = "prompt" value = "prompt" type = "button" onclick = "myFunction()" />  
            <p id = "display"></p>
          
      
        </body>  
      
    </html> 


 以上html代码在页面上显示了三个按钮,点击他们分别弹出alert、confirm、prompt对话框。如果在prompt对话框中输入文字点击确定之后,将会刷新页面,显示出这些文字


  1. import org.openqa.selenium.Alert;  
  2. import org.openqa.selenium.By;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5.   
  6. public class DialogsStudy {  
  7.   
  8.     /** 
  9.      * @wuyepiaoxue
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         System.setProperty("webdriver.firefox.bin","D:/Program Files (x86)/Mozilla Firefox/firefox.exe");    
  14.         WebDriver dr = new FirefoxDriver();  
  15.         String url = "file:///C:/Users/Administrator/Desktop/test.htm";
  16.         dr.get(url);  
  17.           
  18.         //点击第一个按钮,输出对话框上面的文字,然后叉掉  
  19.         dr.findElement(By.id("alert")).click();  
  20.         Alert alert = dr.switchTo().alert();  
  21.         String text = alert.getText();  
  22.         System.out.println(text);  
  23.         alert.dismiss();  
  24.           
  25.         //点击第二个按钮,输出对话框上面的文字,然后点击确认  
  26.         dr.findElement(By.id("confirm")).click();  
  27.         Alert confirm = dr.switchTo().alert();  
  28.         String text1 = confirm.getText();  
  29.         System.out.println(text1);  
  30.         confirm.accept();  
  31.           
  32.         //点击第三个按钮,输入你的名字,然后点击确认,最后  
  33.         dr.findElement(By.id("prompt")).click();  
  34.         Alert prompt = dr.switchTo().alert();  
  35.         String text2 = prompt.getText();  
  36.         System.out.println(text2);  
  37.         prompt.sendKeys("jarvi");  
  38.         prompt.accept();  
  39.           
  40.     }  
  41.   

posted @ 2016-06-17 10:09  午夜飘雪789  阅读(479)  评论(0编辑  收藏  举报