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();
    }

  • 相关阅读:
    对 Excel 工作簿中的数字签名和代码签名的说明
    单例模式
    面向对象
    Des对称加密
    Java获取电脑硬件信息
    鼠标双击事件不可描述的问题
    RSA不对称加密
    JTable表格案例
    控件刷新的奥秘
    反编译插件安装
  • 原文地址:https://www.cnblogs.com/scodong/p/4788241.html
Copyright © 2011-2022 走看看