Java 无接触异常可以';不要扔

Java 无接触异常可以';不要扔,java,selenium-webdriver,Java,Selenium Webdriver,我正在尝试使用一个元素进行测试,如果找不到该元素,我希望继续测试 我在这部分代码中使用了NoTouchElementException 以下是我以前尝试过的: try { WebElement site_width_full_width = wait.until( ExpectedConditions.visibilityOfElementLocated( By.cssSelector("label[for=site_width-full

我正在尝试使用一个元素进行测试,如果找不到该元素,我希望继续测试

我在这部分代码中使用了NoTouchElementException

以下是我以前尝试过的:

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);
}
但是当元素不可用时,它不能被抛出到NosTouchElementException中,所有测试都会中断并失败

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


提前感谢。

您可以尝试使用其父类,如catch块中的Throwable或Exception。在我的例子中,我在catch块中是Throwable的,它按预期工作

您可以尝试使用它的父类,如catch块中的Throwable或Exception。在我的例子中,我被丢弃在catch块中,该块按预期工作

您可能会得到一个不同派生类类型的异常。您可以使用父类“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);
            }
        }

希望这有帮助。

您可能会遇到另一个派生类类型的异常。您可以使用父类“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);
            }
        }

希望这能有所帮助。

您正在捕获一个
NoTouchElementException
,但是如果没有找到任何内容,显式等待将抛出一个
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!");
    }
列出站点\u宽度\u全宽=
driver.findElements(By.cssSelector(“标签[for=site\u width-full\u width]”);
如果(站点宽度全宽度.size()>0){
System.out.println(“找到全宽标签!”);
站点宽度。获取(0)。单击();
}否则{
System.out.println(“未找到全宽标签!”);
}

您正在捕获一个
NoTouchElementException
,但是如果没有找到任何内容,显式等待将抛出一个
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!");
    }
列出站点\u宽度\u全宽=
driver.findElements(By.cssSelector(“标签[for=site\u width-full\u width]”);
如果(站点宽度全宽度.size()>0){
System.out.println(“找到全宽标签!”);
站点宽度。获取(0)。单击();
}否则{
System.out.println(“未找到全宽标签!”);
}

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!");

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

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!");

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

是否在找不到元素时抛出一个
NoSuchElementException
行为?它显示了以下错误:无法定位元素:{“方法”:“css选择器”,“选择器”:“标签[for=site\u width-full\u width]”有关此错误的文档,请访问:这就是我在“无法定位元素”消息是引发的异常的名称之前使用此异常@SmutjeRight的原因。。。它是什么?这就是您需要捕获的,当找不到元素时抛出一个
NoSuchElementException
行为?它向我显示了以下错误:找不到元素:{“方法”:“css选择器”,“选择器”:“label[for=site\u width-full\u width]”以获取有关此错误的文档,请访问:这就是我在“无法定位元素”消息是引发的异常的名称之前使用此异常@SmutjeRight的原因。。。它是什么?这就是你所需要的,我的课很好,谢谢。但我仍然想知道为什么它的特定类(NoSuchElement)不起作用@sreenivasulu您可以尝试调试并在catch块中设置断点,然后可以确定捕获的异常属于哪个类。@AliMozafari有关这些异常层次结构的详细信息,请参阅此页。请参阅肯斯顿于2016年8月17日发表的评论。这只是一个层次结构IssueeException类工作正常,谢谢。但我仍然想知道为什么它的特定类(NoSuchElement)不起作用@sreenivasulu您可以尝试调试并在catch块中设置断点,然后可以确定捕获的异常属于哪个类。@AliMozafari有关这些异常层次结构的详细信息,请参阅此页。请参阅肯斯顿于2016年8月17日发表的评论。这只是一个等级问题