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 mvc中的@PathVariable
    JSP禁用缓存的方式 response.setHeader( "Pragma", "no-cache" ); setDateHeader("Expires", 0);
    request.getSession(true)和request.getSession(false)的区别
    Spring Mvc中DispatcherServlet和Servlet的区别小结
    web.xml中load-on-startup的作用
    Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
    web.xml配置中的log4jRefreshInterval
    web.xml中webAppRootKey
    关于tolua的使用
    关于#pragma pack
  • 原文地址:https://www.cnblogs.com/scodong/p/4788241.html
Copyright © 2011-2022 走看看