zoukankan      html  css  js  c++  java
  • Selenium,处理alert、confirm、prompt对话框 弹层

     1     //点击第一个按钮,输出对话框上面的文字,然后叉掉  
     2     dr.findElement(By.id("alert")).click();  
     3     Alert alert = dr.switchTo().alert();  
     4     String text = alert.getText();  
     5     System.out.println(text);  
     6     alert.dismiss();    8     

    //点击第二个按钮,输出对话框上面的文字,然后点击确认 9 dr.findElement(By.id("confirm")).click(); 10 Alert confirm = dr.switchTo().alert(); 11 String text1 = confirm.getText(); 12 System.out.println(text1); 13 confirm.accept(); 14

    15 //点击第三个按钮,输入你的名字,然后点击确认,最后 16 dr.findElement(By.id("prompt")).click(); 17 Alert prompt = dr.switchTo().alert(); 18 String text2 = prompt.getText(); 19 System.out.println(text2); 20 prompt.sendKeys("jarvi"); 21 prompt.accept();
    从以上代码可以看出dr.switchTo().alert();这句可以得到alertconfirmprompt对话框的对象,然后运用其方法对它进行操作。对话框操作的主要方法有:
     
    • getText()    得到它的文本值
    • accept()      相当于点击它的"确认"
    • dismiss()     相当于点击"取消"或者叉掉对话框
    • sendKeys() 输入值,这个alertconfirm没有对话框就不能用了,不然会报错。
  • 相关阅读:
    [haoi2011]向量
    [haoi2008]硬币购物
    noi2001 [食物链]并查集p1697
    [haoi2012]容易题(数论+容斥的思想)
    p1620田忌赛马(贪心+模拟)
    p1368[扑街]广场铺砖
    [zjoi2010]网络扩容
    是时候写一篇总结了
    P1630 求和
    魔术棋子
  • 原文地址:https://www.cnblogs.com/by170628/p/7121890.html
Copyright © 2011-2022 走看看