Skip to content

Commit 39e10f3

Browse files
[java] fix NoSuchElementException for custom By locators (#17287)
* [java] fix NoSuchElementException for custom By locators Modified ElementLocation to throw NoSuchElementException when a context-based finder returns null. Ensures symmetry with the remote finder and maintains backward compatibility for custom locators. render_diffs(file:///d:/projects/selenium/java/src/org/openqa/selenium/remote/ElementLocation.java) render_diffs(file:///d:/projects/selenium/rb/lib/selenium/webdriver/common/websocket_connection.rb) render_diffs(file:///d:/projects/selenium/rb/lib/selenium/webdriver/devtools/network_interceptor.rb) render_diffs(file:///d:/projects/selenium/rb/lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb) * Retry CLA Assistant workflow --------- Co-authored-by: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com>
1 parent 51739f2 commit 39e10f3

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

java/src/org/openqa/selenium/remote/ElementLocation.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ WebElement findElement(
138138
BiFunction<String, Object, CommandPayload> createPayload,
139139
By locator) {
140140
WebElement element = locator.findElement(context);
141+
if (element == null) {
142+
throw new NoSuchElementException("Unable to find element with locator " + locator);
143+
}
141144
return massage(driver, context, element, locator);
142145
}
143146

@@ -148,6 +151,10 @@ List<WebElement> findElements(
148151
BiFunction<String, Object, CommandPayload> createPayload,
149152
By locator) {
150153
List<WebElement> elements = locator.findElements(context);
154+
if (elements == null) {
155+
return Collections.emptyList();
156+
}
157+
151158
return elements.stream()
152159
.map(e -> massage(driver, context, e, locator))
153160
.collect(Collectors.toList());

0 commit comments

Comments
 (0)