在Selenium WebDriver中按条件中断站点加载

时间:2018-10-16 13:29:52

标签: java selenium selenium-webdriver

有多个文件应上载并等待输出。在处理过程中会打开一个类似AJAX的窗口。如果处理时间太长,请在此窗口上单击 Close 按钮,然后重新提交文件。

我尝试使用下面的代码,但在10秒钟内没有单击关闭按钮。

sales.cbac.com

1 个答案:

答案 0 :(得分:1)

尝试使用“ visibilityOfElementLocated”条件代替“ presenceOfElementLocated”,如下所示:

webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("button-download")));

使用循环:

try {
      driver.findElement(By.id("button-submit")).click();
      Thread.sleep(3000);//3 seconds
      log.info("Processing in progress!");

      for(int i=0; i<10;i++){
        try{
          webElement = driver.findElement(By.className("button-download"));
        } catch (Exception e){e.printStackTrace();}

        if(webElement.isDisplayed())
          break;
        else
          Thread.sleep(1000);
      }

    } catch (TimeoutException ex) {ex.printStackTrace();} finally {
      driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
    }


    if (!webElement.isDisplayed() ) {
      driver.findElement(By.xpath("/html/body/div[4]/div[1]/button/span[1]")).click();
      Thread.sleep(2000);
      driver.findElement(By.id("button-submit")).click();
    }