当前位置: 移动技术网 > IT编程>开发语言>Java > java selenium操作弹出对话框示例讲解

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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']"));
  confirmbutton.click();
  thread.sleep(2000);
  alert javascriptconfirm = driver.switchto().alert();
  javascriptconfirm.accept();
  thread.sleep(2000);
  javascriptconfirm = driver.switchto().alert();
  javascriptconfirm.accept();
 }

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网