InvalidSelectorException 已更改

⚠️ InvalidSelectorException 现在继承自 WebDriverException

在 Java 和 C# 中 Selenium 4.8.2 之前的版本中,当使用无效的定位器来识别元素时,我们的绑定结果行为会不一致。

例如,让我们检查以下代码

ArrayList<Class<? extends Exception>> expectedExceptions = new ArrayList<>();
        expectedExceptions.add(org.openqa.selenium.NoSuchElementException.class);
        expectedExceptions.add(org.openqa.selenium.StaleElementReferenceException.class);
        expectedExceptions.add(org.openqa.selenium.ElementNotInteractableException.class);
        expectedExceptions.add(org.openqa.selenium.InvalidElementStateException.class);
        
return new FluentWait<>(driver)
      .withTimeout(Duration.ofMillis(ELEMENT_IDENTIFICATION_TIMEOUT))
      .pollingEvery(Duration.ofMillis(ELEMENT_IDENTIFICATION_POLLING_DELAY))
      .ignoreAll(expectedExceptions)
      .until(nestedDriver -> {
         nestedDriver.findElement(By.xpath("invalid-xpath")).click;
      });

在此更改之前的预期结果是驱动程序等待直到超时到期,然后抛出 InvalidSelectorException

这没有什么意义,因为损坏/无效的选择器永远不会自行修复,因此应该立即抛出。

这个问题在 2022 年 8 月 17 日的 TLC 会议上讨论并达成一致,并通过拉取请求 11727 和以下 提交 实现。

通过上述更改,无效的选择器将立即抛出 InvalidSelectorException

请注意,如果您在处理无效定位器时不希望抛出此异常,则可能会对向后兼容性产生影响。

请关注 SeleniumHQ,了解最新动态!

测试愉快!