|
| 1 | +--- |
| 2 | +title: "InvalidSelectorException has changed" |
| 3 | +linkTitle: "InvalidSelectorException has changed" |
| 4 | +date: 2023-04-21 |
| 5 | +tags: ["webdriver", "java", "dotnet", "exceptions"] |
| 6 | +categories: ["general"] |
| 7 | +author: Mohab Mohie ([MohabMohie](https://github.com/MohabMohie/MohabMohie)) |
| 8 | +description: > |
| 9 | + ⚠️ `InvalidSelectorException` now extends from `WebDriverException` |
| 10 | +--- |
| 11 | + |
| 12 | +Before Selenium 4.8.2 in Java and C#, when an invalid locator was used to identify an element, the resulting behavior would be |
| 13 | +inconsistent in our bindings. |
| 14 | + |
| 15 | +For example, let's check the following code: |
| 16 | + |
| 17 | +```java |
| 18 | +ArrayList<Class<? extends Exception>> expectedExceptions = new ArrayList<>(); |
| 19 | + expectedExceptions.add(org.openqa.selenium.NoSuchElementException.class); |
| 20 | + expectedExceptions.add(org.openqa.selenium.StaleElementReferenceException.class); |
| 21 | + expectedExceptions.add(org.openqa.selenium.ElementNotInteractableException.class); |
| 22 | + expectedExceptions.add(org.openqa.selenium.InvalidElementStateException.class); |
| 23 | + |
| 24 | +return new FluentWait<>(driver) |
| 25 | + .withTimeout(Duration.ofMillis(ELEMENT_IDENTIFICATION_TIMEOUT)) |
| 26 | + .pollingEvery(Duration.ofMillis(ELEMENT_IDENTIFICATION_POLLING_DELAY)) |
| 27 | + .ignoreAll(expectedExceptions) |
| 28 | + .until(nestedDriver -> { |
| 29 | + nestedDriver.findElement(By.xpath("invalid-xpath")).click; |
| 30 | + }); |
| 31 | +``` |
| 32 | + |
| 33 | +The expected result *before this change* would be that the driver waits until the timeout expires and then throw an `InvalidSelectorException`. |
| 34 | + |
| 35 | +This doesn't make much sense because a broken/invalid selector would never fix itself, and hence should throw immediately. |
| 36 | + |
| 37 | +This was discussed and agreed during the [TLC meeting on August 17, 2022](https://www.selenium.dev/meetings/2022/tlc-08-17/#proposals), |
| 38 | +and implemented through the pull request [11727](https://github.com/SeleniumHQ/selenium/pull/11727) and the following |
| 39 | +[commit](https://github.com/SeleniumHQ/selenium/commit/f28144eb72ae1df18f267a5250db6b9b41dc1fdc). |
| 40 | + |
| 41 | +With the changes mentioned above, an invalid selector will throw an `InvalidSelectorException` immediately. |
| 42 | + |
| 43 | +Please note that this may have an impact on backwards compatibility if you are not expecting this exception to be thrown while |
| 44 | +handling invalid locators. |
| 45 | + |
| 46 | +Stay tuned for updates by following [SeleniumHQ](https://twitter.com/seleniumhq)! |
| 47 | + |
| 48 | +Happy testing! |
| 49 | + |
0 commit comments