【问题标题】:Selenium no such element exception handlingSelenium 无此类元素异常处理
【发布时间】:2020-07-07 12:30:47
【问题描述】:

我已经设置了一个方法来根据元素是否存在返回布尔值,但是它不能正常工作。另外,我已经导入了 org.openqa.selenium.NoSuchElementException;但是没用过

方法

public static boolean checkElement(WebDriver driver, WebElement element) {
        return element.isDisplayed()  || element.isEnabled();
    }

【问题讨论】:

    标签: java selenium web-scraping


    【解决方案1】:

    添加Java Exceptions - Try...Catch:

    public static boolean checkElement(WebDriver driver, WebElement element) {
        try {
            return element.isDisplayed()  || element.isEnabled();
        } catch (Exception e) {
            return false;
        }
    }
    

    【讨论】:

      【解决方案2】:

      有不同的处理方式:

      1. 这里是通过findElements检查,如果没有找到元素则列表大小为零,如果找到元素则大小大于零。
                  List<WebElement> elements = driver.findElements(By.xpath(xpathLocation));
                  if (elements.size() > 0) {
                      LOGGER.info("{} is present on the DOM.", id);
                      return true;
                  } else {
                      LOGGER.info("{} is not present on the DOM.", id);
                      return false;
                  }
      
      1. 在这里,如果找到该元素,那么它会将标志返回为 true,如果没有,并且在 NoSuchElementException 的情况下,它也会被处理并且你会得到 false。
          /**
           * Method to check if element is present.
           *
           * @param element
           * @return
           */
          public static boolean isElementPresent(WebElement element) {
              try {
                  if (element.isDisplayed() || element.isEnabled())
                      return true;
              } catch (Exception e) {
                  return false;
              } 
              return false;
          }
      

      【讨论】:

      • 如果我在实现这个方法之前得到了元素,是不是已经在 driver.findElement 出现了错误
      • 是的。我认为我需要更改异常,但这将在元素不可见或其他情况下有所帮助。
      • 我尝试了选项 2 和 1。由于某种原因,选项 1 总是返回大小 0。选项 2 在创建 webelement 时不会抛出这样的元素
      【解决方案3】:

      我以前的方法存在问题,它需要在方法之前创建元素,这将引发错误 no such element 异常。通过将 xpath 添加到方法中,它将解决问题并在未找到时进入 catch 部分。

      public static boolean checkElement(WebDriver driver, String xpath) {
           boolean flag = false;
              try {
                  WebElement element = driver.findElement(By.xpath(xpath));
                  if (element.isDisplayed() || element.isEnabled())
                      flag = true;
              } catch (NoSuchElementException e) {
                  flag = false;
              } 
              return flag;
      }
      

      【讨论】:

        【解决方案4】:

        根据Selenium 客户端元素存在 是一个不确定的状态。相反,您需要探测WebElement 是否属于以下状态:

        -现在 -可见 -可交互

        验证一个WebElement的不同阶段和各自的ExpectedConditions如下:

        • Presence of an element

          presenceOfElementLocated(By locator)
          An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
          
        • Visibility of an element

          visibilityOf(WebElement element)
          An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
          
        • Element to be Clickable

          elementToBeClickable(By locator)
          An expectation for checking an element is visible and enabled such that you can click it.
          

        注意:根据文档Element is Clickable - it is Displayed and Enabled


        这个用例

        验证WebElement的不同状态:

        • 存在

          WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("element_xpath")));
          
        • 可见性

          WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(element));
          
        • 交互性

          WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element));
          

        【讨论】:

          猜你喜欢
          • 2016-10-27
          • 1970-01-01
          • 2021-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多