Selenium+Java(七)Selenium对话框的处理

songerxing 2019-12-03

HTML代码如图所示:

Selenium+Java(七)Selenium对话框的处理

一、alert

String url = "file:///C:/Users/ex_yuhao/Desktop/index.html";
//引用IE浏览器驱动
System.setProperty("webdriver.ie.driver", "./src/driver/IEDriverServer.exe");
//创建IE浏览器对象
InternetExplorerDriver driver = new InternetExplorerDriver();
//浏览器最大化
driver.manage().window().maximize();
//等待浏览器加载完毕
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//打开网站
driver.get(url);
//点击提示框1
driver.findElement(By.id("button1")).click();
//获取alert对象
Alert alert = driver.switchTo().alert();
//点击提示框按钮
alert.dismiss();
//强制等待1S
Thread.sleep(1000);
//关闭
driver.close();

二、confirm

String url = "file:///C:/Users/ex_yuhao/Desktop/index.html";
//引用IE浏览器驱动
System.setProperty("webdriver.ie.driver", "./src/driver/IEDriverServer.exe");
//创建IE浏览器对象
InternetExplorerDriver driver = new InternetExplorerDriver();
//浏览器最大化
driver.manage().window().maximize();
//等待浏览器加载完毕
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//打开网站
driver.get(url);
//点击提示框2
driver.findElement(By.id("button2")).click();
//获取提示框对象
Alert confirm = driver.switchTo().alert();
//点击提示框取消按钮,确认为accept()方法。
confirm.dismiss();
Thread.sleep(1000);
driver.close();

三、prompt

String url = "file:///C:/Users/ex_yuhao/Desktop/index.html";
//引用IE浏览器驱动
System.setProperty("webdriver.ie.driver", "./src/driver/IEDriverServer.exe");
//创建IE浏览器对象
InternetExplorerDriver driver = new InternetExplorerDriver();
//浏览器最大化
driver.manage().window().maximize();
//等待浏览器加载完毕
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//打开网站
driver.get(url);
//点击提示框2
driver.findElement(By.id("button3")).click();
//获取提示款对象
Alert prompt= driver.switchTo().alert();
//输入框赋值
prompt.sendKeys("提示框");
//点击提示框确认,取消为dismiss()方法。
prompt.accept();
//等待3S
Thread.sleep(3000);
//关闭
driver.close();

相关推荐