并不总是单击复选框

时间:2017-05-31 14:13:22

标签: selenium selenium-webdriver webdriver automated-tests

我正在尝试自动执行涉及下载月度报告的流程。

    //Set Call handler
    Select handlerdd = new Select(driver.findElement(By.cssSelector("#searchType")));
    handlerdd.selectByValue("select");

    //Wait
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    //Search, choose is exactly as the filtering option
    Select searchcriteriadd = new Select(driver.findElement(By.cssSelector("table.content-form:nth-child(1) > tbody:nth-child(1) > "
            + "                                                             tr:nth-child(1) > td:nth-child(1) > select:nth-child(2)")));
    searchcriteriadd.selectByVisibleText("Is Exactly");

    //Search box
    WebElement search = driver.findElement(By.cssSelector("#searchString"));
    search.sendKeys("File I Want");
    search.sendKeys(Keys.ENTER);

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    //Explicitly wait until element is seen
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("td.content-grid-th-delete > input:nth-child(1)")));
    //Checkbox
    driver.findElement(By.cssSelector("td.content-grid-th-delete > input:nth-child(1)")).click();

   //Wait 
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    //Generate Report button click
    driver.findElement(By.cssSelector("fieldset.content-buttonbar:nth-child(10) > legend:nth-child(1) > button:nth-child(1)")).click();

该过程如下

  1. 从下拉菜单(handlerdd)中选择选项并显示搜索栏。

  2. 搜索过滤器设置为" IsExactly"和我正在寻找的文本"文件"发送到文本字段,然后输入。

  3. 有一个选项,并选中该选项的复选框。
  4. 然后点击生成报告按钮。
  5. 我不完全确定问题是什么。可能发生的一件事(除了我的复选框不起作用)是在对文件进行勾选之前点击生成报告按钮。我不完全确定如何避免这个问题。我应该增加隐含的等待时间吗?

1 个答案:

答案 0 :(得分:0)

在您的情况下,WebDriver会在加载过滤后的数据之前找到“生成报告”按钮。

您尝试使用Thread.sleep();。以下代码可以帮助您。

//Set Call handler
Select handlerdd = new Select(driver.findElement(By.cssSelector("#searchType")));
handlerdd.selectByValue("select");

//Wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

//Search, choose is exactly as the filtering option
Select searchcriteriadd = new Select(driver.findElement(By.cssSelector("table.content-form:nth-child(1) > tbody:nth-child(1) > "
        + "                                                             tr:nth-child(1) > td:nth-child(1) > select:nth-child(2)")));
searchcriteriadd.selectByVisibleText("Is Exactly");

//Search box
WebElement search = driver.findElement(By.cssSelector("#searchString"));
search.sendKeys("File I Want");
search.sendKeys(Keys.ENTER);

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

//Explicitly wait until element is seen
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("td.content-grid-th-delete > input:nth-child(1)")));

//Checkbox
    driver.findElement(By.cssSelector("td.content-grid-th-delete > input:nth-child(1)")).click();

    Thread.sleep(7000);//You can specify time according to your load time

    //Generate Report button click
    driver.findElement(By.cssSelector("fieldset.content-buttonbar:nth-child(10) > legend:nth-child(1) > button:nth-child(1)")).click();

让我知道它是否有效。