【问题标题】:How can I ignore the exception if the item is not found? Selenium(selenide)如果找不到该项目,我该如何忽略异常?硒(硒化物)
【发布时间】:2022-01-15 01:51:43
【问题描述】:

我的一些测试参考了 importBrokerCheckBox。但并不是所有的测试都必须找到它,因为他必须缺席。也就是说,如果该元素在页面上,那么我们必须点击它,如果它不存在,我们必须进一步通过代码。但是下面的代码不起作用并抛出异常 - xpath not found

importBrokerCheckBox = $x(".//div[text()='Брокер']/../../../preceding-sibling::span//span[@aria-label=\"caret-down\"]"),

    if (importBrokerCheckBox.isDisplayed());
    {
        importBrokerCheckBox.click();
    }

    if (importBrokerSettingsCheckbox.isDisplayed());
    {
        importBrokerSettingsCheckbox.click();
    }

【问题讨论】:

    标签: java selenium qa selenide


    【解决方案1】:

    基本上有两种选择:

    1. try/catch 块 - 如果未找到元素(NoSuchElementException 发生),则执行 catch 块代码(可以为空 ofc)。
    2. 使用List<WebElement> elements = driver.findElements(By.by);,如果没有找到任何元素,NoSuchElementException 不会被抛出,并且列表像new ArrayList<WebElement>() 一样保持为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-30
      • 2018-08-16
      • 2015-06-11
      • 2021-08-20
      • 2021-07-14
      • 2011-09-12
      相关资源
      最近更新 更多