Selenium+Java(七)Selenium对话框的处理
HTML代码如图所示:
一、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();
相关推荐
xiangxiaojun 2020-09-23
Reiki 2020-08-16
letheashura 2020-08-14
tiankele0 2020-07-29
curiousL 2020-07-18
王练 2020-07-18
tiankele0 2020-07-18
amei0 2020-07-08
Reiki 2020-07-06
Ronnyxie 2020-07-06
xiangxiaojun 2020-07-05
zhanghaibing00 2020-06-28
xiongyouqiang 2020-06-28
Ronnyxie 2020-06-27
amei0 2020-06-26
letheashura 2020-06-26
tiankele0 2020-06-26
王练 2020-06-25