Selenium的弹窗处理!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium的弹窗处理!相关的知识,希望对你有一定的参考价值。

Selenium的弹窗处理!

对话框相信大家都不陌生,常见的对话框为三种alert、confirm、prompt。这些对话框对Selenium来说不算是界面层的东西。因为他是JS做的。看到这是不是想到了利用之前的JS代码了?不过Selenium解决了这个问题,给我们提供了三个基本方法:accept();、dismiss();、sendKeys();、等方法。这三个分别对应确认、取消、输入。通过这三个方法我们可以实现对JS对话框进行操作。

技术分享图片
技术分享图片
技术分享图片

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class alertConfirmPromptDemo {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver=new ChromeDriver();
        Dimension di=new Dimension(600, 600);
        driver.manage().window().setSize(di);
        driver.get("file:///D:/wb-zq379240/eclipse-workspace/softlyDemo/htmlDemo/demo.html");
//      alert弹窗处理   

//      driver.findElement(By.id("alert")).click();
//      driver.switchTo().alert().accept(); //点击确认

//      //confirm弹窗处理   
//      driver.findElement(By.id("confirm")).click();
//      driver.switchTo().alert().dismiss();   //点击确认
//      Thread.sleep(2000);
//      driver.findElement(By.id("confirm")).click();
//      driver.switchTo().alert().accept();   //点击取消

//      //promot弹窗处理    
//      driver.findElement(By.id("promot")).click();  
//      System.out.println(driver.switchTo().alert().getText());//获取弹窗提示文本
//      driver.switchTo().alert().sendKeys("你狠棒棒");  //想窗口输入文本
//      driver.switchTo().alert().accept();  //点击确认

    }

}

以上是关于Selenium的弹窗处理!的主要内容,如果未能解决你的问题,请参考以下文章

selenium+python处理广告弹窗

selenium自动化之处理浏览器警告弹窗

Selenium2+Python自动化-处理浏览器弹窗(转载)

Selenium2+Python自动化-处理浏览器弹窗(转载)

python selenium处理alert弹出对话框并处理无弹窗判断

python selenium处理alert弹出对话框并处理无弹窗判断