selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框

alert、confirm、prompt这样的js对话框在selenium1.X时代也是难啃的骨头,常常要用autoit来帮助处理。

试用了一下selenium webdriver中处理这些对话框十分方便简洁。以下面html代码为例:

Html代码
  1. Dialogs.html 
Dialogs.html
Html代码
  1. <html> 
  2.  
  3.     <head> 
  4.  
  5.         <title>Alert</title> 
  6.  
  7.     </head> 
  8.  
  9.     <body> 
  10.  
  11.         <inputid = "alert"value = "alert"type = "button"onclick = "alert('欢迎!请按确认继续!');"/> 
  12.     <inputid = "confirm"value = "confirm"type = "button"onclick = "confirm('确定吗?');"/> 
  13.     <inputid = "prompt"value = "prompt"type = "button"onclick = "var name =prompt('请输入你的名字:','请输入 
  14.  
  15. 你的名字'); document.write(name) "/> 
  16.      
  17.  
  18.     </body> 
  19.  
  20. </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('确定吗?');"/>
	<input id = "prompt" value = "prompt" type = "button" onclick = "var name = prompt('请输入你的名字:','请输入

你的名字'); document.write(name) "/>
	

    </body>

</html>

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

selenium webdriver 处理这些弹层的代码如下:

Java代码
  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.      * @author gongjf
  10.      */ 
  11.     publicstatic void main(String[] args) { 
  12.         // TODO Auto-generated method stub 
  13.         System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  14.         WebDriver dr = new FirefoxDriver(); 
  15.         String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Dialogs.html";//"/Your/Path/to/main.html" 
  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.  
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DialogsStudy {

	/**
	 * @author gongjf
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
		WebDriver dr = new FirefoxDriver();
		String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Dialogs.html";// "/Your/Path/to/main.html"
		dr.get(url);
		
		//点击第一个按钮,输出对话框上面的文字,然后叉掉
		dr.findElement(By.id("alert")).click();
		Alert alert = dr.switchTo().alert();
		String text = alert.getText();
		System.out.println(text);
		alert.dismiss();
		
		//点击第二个按钮,输出对话框上面的文字,然后点击确认
		dr.findElement(By.id("confirm")).click();
		Alert confirm = dr.switchTo().alert();
		String text1 = confirm.getText();
		System.out.println(text1);
		confirm.accept();
		
		//点击第三个按钮,输入你的名字,然后点击确认,最后
		dr.findElement(By.id("prompt")).click();
		Alert prompt = dr.switchTo().alert();
		String text2 = prompt.getText();
		System.out.println(text2);
		prompt.sendKeys("jarvi");
		prompt.accept();
		
	}

}

从以上代码可以看出dr.switchTo().alert();这句可以得到alert\confirm\prompt对话框的对象,然后运用其方法对它进行操作。对话框操作的主要方法有:

  • getText()    得到它的文本值
  • accept()      相当于点击它的"确认"
  • dismiss()     相当于点击"取消"或者叉掉对话框
  • sendKeys() 输入值,这个alert\confirm没有对话框就不能用了,不然会报错。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值