2017-05-02 93 views
0

我想通过分页导航每个页面来验证表中的行中的元素。我能够导航到每个页面并断言元素,但是问题是在最后一页,即使Next链接变灰,循环仍然继续。点击下一步链接直到分页在硒webdriver禁用下一个动作

当接下来链路被禁用

<span> 
    <a class="next_btn inactive">NEXT ></a> 
    </span> 

当接下来链接启用

<span> 
    <a class="next_btn" href="home.do?action=Next&start=10&end=20&sort=&   
    type=00&status=&fromDate=04/02/2017&toDate=05/02/2017&school=&  
    district=0009">NEXT ></a> 
    </span> 

实际代码

public void submissionType() throws Exception { 
    driver.findElement(By.linkText("NEXT >")); 
    while(true) { 
     processPage(); 
     if (pagination.isDisplayed() && pagination.isEnabled()){ 
      pagination.click(); 
      Thread.sleep(100L); 
     } else 
      break; 

    } 
    } 

    private void processPage() { 
    String statusColumn="//td[@class='gridDetail'][2]"; 
    List<WebElement> list = table.findElements(By.xpath(statusColumn)); 
    for (WebElement checkbox : list) { 
     String columnName=checkbox.getText(); 
     Asserts.assertThat(columnName,"File"); 
    } 
    } 

回答

0

而不是用By.linkText("NEXT >")标识元素,请尝试使用By.cssSelector("a.next_btn")来标识它。

当你使用这种方法时,当对象被禁用时,它的类名会改变,因此你的对象将不再被识别并且你的循环会中断。

编辑:添加try块和catch NoSuchElement异常以避免异常。

+0

'当您使用此方法,那么当对象被禁用,它的类名会改变,因此你的对象将不再得到识别并且你的循环会中断。“这并不是真的......即使是禁用按钮也可以通过CSS选择器”a.next_btn“找到。 – JeffC

+0

当对象被禁用时,CSS不应该是a.next_btn.inactive吗?如果我们使用a.next_btn –

+1

'a.next_btn.inactive'会在禁用时找到该按钮,但是会发现'a.next_btn'也会得到部分类名。 'next_btn'和'inactive'是两个不同的类。当它被禁用时,'a.inactive'也会找到按钮。 – JeffC

0

我知道你已经接受了答案,但其中一个陈述是不正确的。定位器By.cssSelector("a.next_btn")将找到启用和禁用按钮,因此它不会导致循环中断。

看看你的代码,我会提供一些建议/更正。

  1. .isEnabled()才真正适用于INPUT标记,以便测试为不真的在这里完成任何事情。

  2. 使用Thread.sleep()不是一个好习惯。你可以谷歌一些解释,但基本上问题是,这是一个僵化的等待。如果您正在等待的元素在15ms内变为可用,则您仍将等待10秒或无论您的睡眠设置为何。使用明确的等待(WebDriverWait)是最佳做法。

我会收拾你的函数,并将它们写这样

public void submissionType() 
{ 
    WebDriverWait wait = new WebDriverWait(driver, 10); 
    By nextButtonLocator = By.cssSelector("a.next_btn:not(.inactive)"); 
    List<WebElement> nextButton = driver.findElements(nextButtonLocator); 
    while (!nextButton.isEmpty()) 
    { 
     processPage(); 
     nextButton.get(0).click(); 
     wait.until(ExpectedConditions.stalenessOf(nextButton.get(0))); // wait until the page transitions 
     nextButton = driver.findElements(nextButtonLocator); 
    } 
} 

private void processPage() 
{ 
    for (WebElement checkbox : table.findElements(By.xpath("//td[@class='gridDetail'][2]"))) 
    { 
     Asserts.assertThat(checkbox.getText(), "File"); 
    } 
} 
相关问题