栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java selenium操作弹出对话框示例讲解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java selenium操作弹出对话框示例讲解

Web 开发人员通常需要利用Javascript弹出对话框来给用户一些信息提示, 包括以下几种类型

阅读目录

  1. 对话框类型
  2. 测试页面
  3. Selenium 操作对话框的代码

对话框类型

1.  警告框: 用于提示用户相关信息的验证结果, 错误或警告等

2. 提示框: 用于提示用户在当前对话框中输入数据,一般需要用户单击取消或者确认按钮

3. 确认框: 用于提示用户确认或者取消某个操作,一般需要用户单击取消或者确认按钮

测试页面

用如下页面为例进行讲解,  包括了警告框,提示框,确认框

http://sislands.com/coin70/week1/dialogbox.htm

Selenium 操作对话框的代码

 public static void testalert(WebDriver driver)
 {
  String url="http://sislands.com/coin70/week1/dialogbox.htm";
  driver.get(url);
  
  WebElement alertButton = driver.findElement(By.xpath("//input[@value='alert']"));
  alertButton.click();
  
  alert javascriptalert = driver.switchTo().alert();
  System.out.println(javascriptalert.getText());
  javascriptalert.accept();
 }
 
 public static void testprompt(WebDriver driver) throws Exception
 {
  String url="http://sislands.com/coin70/week1/dialogbox.htm";
  driver.get(url);
  
  WebElement promptButton = driver.findElement(By.xpath("//input[@value='prompt']"));
  promptButton.click();
  Thread.sleep(2000);
  alert javascriptprompt = driver.switchTo().alert();
  javascriptprompt.sendKeys("This is learning Selenium");
  javascriptprompt.accept(); 
  
  System.out.println(javascriptprompt.getText());
  
  javascriptprompt=driver.switchTo().alert();
  javascriptprompt.accept();
  
  Thread.sleep(2000);
  promptButton.click();
  javascriptprompt=driver.switchTo().alert();
  javascriptprompt.dismiss();
  Thread.sleep(2000);
  javascriptprompt=driver.switchTo().alert();
  javascriptprompt.accept();
 }
 
 public static void testConfirm(WebDriver driver) throws Exception
 {
  String url="http://sislands.com/coin70/week1/dialogbox.htm";
  driver.get(url);
  
  WebElement confirmButton = driver.findElement(By.xpath("//input[@value='/confirm/i']"));
  /confirm/iButton.click();
  Thread.sleep(2000);
  alert javascriptConfirm = driver.switchTo().alert();
  javascript/confirm/i.accept();
  Thread.sleep(2000);
  javascriptConfirm = driver.switchTo().alert();
  javascript/confirm/i.accept();
 }

 以上就是对 java selenium操作弹出对话框的资料整理,后续继续补充,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号