2727import org .openqa .selenium .InvalidArgumentException ;
2828import org .openqa .selenium .NoSuchElementException ;
2929import org .openqa .selenium .SearchContext ;
30+ import org .openqa .selenium .WebDriverException ;
3031import org .openqa .selenium .WebElement ;
3132import 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