繁体   English   中英

如何在Selenium中跳过NoSuchElementException

[英]How to skip NoSuchElementException in Selenium

我有一种情况,我想单击图表(“响应”)上的条形,然后显示一个关系条形图(“响应“付款(是)””))。 当我单击关系条形图上的条形图时,它会显示合同列表。 这在下面的屏幕截图中突出显示 截图

我遇到的问题是,“响应”图的“付款要约(是)”部分没有栏。 这是一个有效的测试方案,但随后发生的是右侧的关系条形图未显示任何数据。 此外,当我的硒代码然后从列表中查找合同但找不到​​合同时,测试将失败,并显示“ NoSuchElementException”。 我知道为什么会这样做,但是我的问题是...。是否有一种方法可以调整代码以适应这种情况,以便在找不到关系图(右侧图)的情况下,以及,下表中的合同清单,代码可以跳过吗?

我在下面附加了我的代码的详细信息:

1)此方法正在尝试从表中的列表中打开/关闭合同。 可以通过某种方式对此进行修改,以便在不显示合同的情况下(这是有效的情况),代码可以跳过NoSuchElementException吗?

    public static void openCloseContractForReportWithBarChart(InternetExplorerDriver driver) throws InterruptedException {
    String winHandleBefore = driver.getWindowHandle();
    waitCommands.fluentWaitOnContractSelect(driver);

    for(String winHandle : driver.getWindowHandles()) {
       if(!winHandle.equals(winHandleBefore)){
                try{
                    Thread.sleep(4000);
                   }catch(InterruptedException e){
                   }
                driver.switchTo().window(winHandle);
                break;
            }
    }       
    driver.close();
    driver.switchTo().window(winHandleBefore);
}

2)在上面的方法中,您会注意到,我已经引用了“ FluentWaitOnContractSelect”方法。 详细信息如下:

    public static void fluentWaitOnContractSelect(InternetExplorerDriver driver)
{

    WebElement selectContractAfterWait = (new WebDriverWait(driver, 15))
            .until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='tabular-breakdown']/table/tbody/tr[1]/td[1]/a")));
    selectContractAfterWait.click();
}

任何帮助将不胜感激。

据我所知,您不能跳过元素,因为不知道何时找不到确切的元素,所以最好的方法是告诉Web驱动程序等到找到下一个元素,请为此提供以下代码:

 WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.id[elementlocator]));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM