selenium获取alert
①只有确认按钮的弹框
WebDriver driver = new ChromeDriver(); driver.get("http://xxx"); Alert alert = driver.switchTo().alert(); alert.accept();
②有确认和取消按钮的弹框
WebDriver driver = new ChromeDriver(); driver.get("http://xxx"); Alert alert = driver.switchTo().alert(); alert.accept();//点击确认按钮 alert.dismiss();//点击取消按钮
③有输入框的弹框
WebDriver driver = new ChromeDriver(); driver.get("http://xxx"); Alert alert = driver.switchTo().alert(); alert.sendKeys("测试"); alert.accept();
④有提醒语的弹框
WebDriver driver = new ChromeDriver(); driver.get("http://xxx"); Alert alert = driver.switchTo().alert(); String text = alert.getText();//获取弹框的提示语 alert.accept();
Alert a = driver.switchTo().alert(); 此方法可以获取当前页面弹出的alert
a.getText(); 获取提示框的文本内容
a.accept(); 点击提示框的确定按钮
a.dismiss(); 点击提示框的取消按钮