无法引发NoSuchElementException

时间:2019-05-20 07:06:17

标签: java selenium-webdriver

我正在尝试使用一个元素进行测试,并且即使找不到该元素,我也想继续进行测试。

我在这部分代码中使用了NoSuchElementException。

这是我之前尝试过的:

try {
    WebElement site_width_full_width = wait.until( 
            ExpectedConditions.visibilityOfElementLocated(
            By.cssSelector("label[for=site_width-full_width]")));
    site_width_full_width.click();

    Thread.sleep(1000);
    System.out.println("FullWidth Label Found!");

} catch (NoSuchElementException e) {
    System.out.println("FullWidth Label not found!");
    System.out.println(e);
}

但是当该元素不可用时,就不能将其扔到NoSuchElementException中,并且所有测试都会失败并失败。

解决方案是什么,当元素不可用时如何继续测试。

谢谢。

4 个答案:

答案 0 :(得分:1)

您可以在catch块中尝试使用其父类(如Throwable或Exception)。就我而言,我可以按预期的方式在捕获块中抛出

答案 1 :(得分:1)

您可能会遇到其他派生类类型的异常。您可以使用父类“ Exception”来捕获它,然后进一步细化确切的异常类型。

尝试使用;

try
{
            WebElement site_width_full_width = wait.until(
                    ExpectedConditions.visibilityOfElementLocated(
                    By.cssSelector("label[for=site_width-full_width]")));
            site_width_full_width.click();

            Thread.sleep(1000);
            System.out.println("FullWidth Label Found!");

        }
        catch (Exception e)
        {
            if (e instanceof NoSuchElementException)
            {
                System.out.println("FullWidth Label not found!");
                System.out.println(e);
            }
            else
            {
                System.out.println("Unexpected exception!");
                System.out.println(e);
            }
        }

希望这会有所帮助。

答案 2 :(得分:0)

您正在捕获NoSuchElementException,但是如果找不到任何内容,则显式等待将引发TimeoutException。要获得您的工作,您应该修改代码以执行以下操作:

    try {
        WebElement site_width_full_width = wait.until(
                ExpectedConditions.visibilityOfElementLocated(
                        By.cssSelector("label[for=site_width-full_width]")
                ));
        site_width_full_width.click();
        System.out.println("FullWidth Label Found!");
    } catch (NoSuchElementException | TimeoutException e) {
        System.out.println("FullWidth Label not found!");
        System.out.println(e);
    }

但是,使用Try / Catch作为执行流程通常是一种代码反模式。做这样的事情会更好:

    List<WebElement> site_width_full_width = 
            driver.findElements(By.cssSelector("label[for=site_width-full_width]"));
    if (site_width_full_width.size() > 0) {
        System.out.println("FullWidth Label Found!");
        site_width_full_width.get(0).click();
    } else {
        System.out.println("FullWidth Label not found!");
    }

答案 3 :(得分:0)

Webdriver已经提供了以更简单的方式解决此问题的方法。您可以使用以下方式

 WebDriverWait wait= new WebDriverWait(driver, TimeSpan.FromSeconds(120));
    wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));

 WebElement site_width_full_width = wait.until( 
            ExpectedConditions.visibilityOfElementLocated(
            By.cssSelector("label[for=site_width-full_width]")));
    site_width_full_width.click();

    Thread.sleep(1000);
    System.out.println("FullWidth Label Found!");

注意:您可以添加所有需要忽略的异常类型。

相关问题