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 和以下 commit 實作。

透過上述變更,無效的選擇器將立即拋出 InvalidSelectorException

請注意,如果您在處理無效的定位器時未預期會拋出此例外,則這可能會對向後相容性產生影響。

請關注 SeleniumHQ,以隨時掌握最新資訊!

測試愉快!

上次修改時間:2023 年 4 月 24 日:發布關於 InvalidSelectorException 的文章 (481e8883196)