zoukankan      html  css  js  c++  java
  • Selenium webdriver如何处理confirm对话框的问题

    我在学习selenium webdriver中处理alert, confirm, prompt的时候遇到个棘手的问题,就是程序执行的太快,导致没有跳转到alert对话框就执行完了,结果99%的时候执行失败,偶尔会执行成功。下面贴出代码供大家参考:

    @Test
    public void testConfirmDialog() throws Exception
    {
    driver.manage().window().maximize();
    WebElement promptBtn = driver.findElement(By.xpath("//input[@value = 'confirm']"));
    promptBtn.click();
    Alert jsConfirm = driver.switchTo().alert();
    jsConfirm.accept();

    jsConfirm = driver.switchTo().alert();
    log.info(jsConfirm.getText());
    jsConfirm.accept();

    promptBtn.click();
    jsConfirm = driver.switchTo().alert();
    jsConfirm.dismiss();

    jsConfirm = driver.switchTo().alert();
    log.info(jsConfirm.getText());
    jsConfirm.accept();
    }

    上述代码在单步调试的时候没有任何问题,但是在run的时候就会失败,偶尔会有成功的时候。非常郁闷

    虽然网上都不推荐使用Thread.sleep,但我也没找到其他好的方法,就是在点击按钮之后,加一个Thread.sleep(2000),让程序等待两秒再执行。
    @Test
    public void testConfirmDialog() throws Exception
    {
    driver.manage().window().maximize();
    WebElement promptBtn = driver.findElement(By.xpath("//input[@value = 'confirm']"));
    promptBtn.click();
    try{
    Thread.sleep(INTERVAL_TIME);
    }catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    Alert jsConfirm = driver.switchTo().alert();
    jsConfirm.accept();

    try{
    Thread.sleep(INTERVAL_TIME);
    }catch (InterruptedException e)
    {
    e.printStackTrace();
    }

    jsConfirm = driver.switchTo().alert();
    log.info(jsConfirm.getText());

    jsConfirm.accept();

    promptBtn.click();
    try{
    Thread.sleep(INTERVAL_TIME);
    }catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    jsConfirm = driver.switchTo().alert();
    jsConfirm.dismiss();

    try{
    Thread.sleep(INTERVAL_TIME);
    }catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    jsConfirm = driver.switchTo().alert();
    log.info(jsConfirm.getText());
    jsConfirm.accept();
    }

  • 相关阅读:
    Spring Boot 打包插件,真是太有用了!
    java高级应用:线程池全面解析
    漫画:HTTP 协议极简教程,傻瓜都能看懂!
    Tomcat 连接数与线程池详解
    Intellij IDEA Debug 调试技巧
    Java 程序员必须掌握的 5 个注解!
    如何优雅地终止一个线程?
    springmvc实现REST中的GET、POST、PUT和DELETE
    @Resource 和 @Autowired注解的异同
    SpringMVC的各种参数绑定方式
  • 原文地址:https://www.cnblogs.com/scodong/p/4788241.html
Copyright © 2011-2022 走看看