Skip to content

Commit 9667310

Browse files
joerg1985asolntsev
authored andcommitted
[java] avoid ClassCastException for unexpected driver responses #16389
1 parent 69bbda9 commit 9667310

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openqa.selenium.InvalidArgumentException;
2828
import org.openqa.selenium.NoSuchElementException;
2929
import org.openqa.selenium.SearchContext;
30+
import org.openqa.selenium.WebDriverException;
3031
import org.openqa.selenium.WebElement;
3132
import org.openqa.selenium.internal.Require;
3233

@@ -163,7 +164,7 @@ WebElement findElement(
163164
CommandPayload commandPayload = createPayload.apply(params.using(), params.value());
164165

165166
Response response = driver.execute(commandPayload);
166-
WebElement element = (WebElement) response.getValue();
167+
Object element = response.getValue();
167168
if (element == null) {
168169
throw new NoSuchElementException("Unable to find element with locator " + locator);
169170
}
@@ -181,7 +182,7 @@ List<WebElement> findElements(
181182

182183
Response response = driver.execute(commandPayload);
183184
@SuppressWarnings("unchecked")
184-
List<WebElement> elements = (List<WebElement>) response.getValue();
185+
List<?> elements = (List<?>) response.getValue();
185186

186187
if (elements == null) { // see https://github.com/SeleniumHQ/selenium/issues/4555
187188
return Collections.emptyList();
@@ -206,9 +207,21 @@ abstract List<WebElement> findElements(
206207
By locator);
207208

208209
protected WebElement massage(
209-
RemoteWebDriver driver, SearchContext context, WebElement element, By locator) {
210-
if (!(element instanceof RemoteWebElement)) {
211-
return element;
210+
RemoteWebDriver driver, SearchContext context, Object element, By locator) {
211+
if (!(element instanceof WebElement)) {
212+
String hint;
213+
214+
if (element instanceof Map<?, ?>) {
215+
hint = ((Map<?, ?>) element).keySet().toString();
216+
} else if (element != null) {
217+
hint = element.getClass().getName();
218+
} else {
219+
hint = "null";
220+
}
221+
222+
throw new WebDriverException("unexpected driver response: " + hint);
223+
} else if (!(element instanceof RemoteWebElement)) {
224+
return (WebElement) element;
212225
}
213226

214227
RemoteWebElement remoteElement = (RemoteWebElement) element;

0 commit comments

Comments
 (0)