在webtable中搜索元素时获取异常

时间:2016-08-05 04:41:31

标签: java selenium webdriver

我试图在网页表中找到元素。

当找不到元素时,我无法通过测试失败

我写了这样的代码

@Test
public void test1() {
    String s = "16";
    List<WebElement> list = driver.findElements(By.xpath("//table[@cellpadding='10' and @border='1']/descendant::tr"));
    int rowSize = list.size();
    System.out.println("rowsize " + rowSize);
    String xpath2 = "//table[@cellpadding='10' and @border='1']/child::tbody/child::tr[1]/child::td";
    List<WebElement> list2 = driver.findElements(By.xpath(xpath2));
    int colSize = list2.size();

    try {
        for (int x = 1; x < rowSize; x++) {
            //x += 1;
            for (int y = 1; y < colSize; y++) {
                //y += 1;
                String xpath = "//table[@cellpadding='10' and @border='1']/child::tbody/child::tr[" + x + "]/child::td[" + y + "][contains(.,'" + s + "')]";
                WebElement we = driver.findElement(By.xpath(xpath));
                if (!we.getText().equals(s))
                    throw new ExplicitAssertionError("element not found");
            }
        }
    } catch (Exception ex) {
        System.out.println("element not found");
    }
}

我正在NoSuchElementException

感谢您的时间

0 个答案:

没有答案