[🐛 Bug]: Randomly getting java.lang.ClassCastException when trying to interact with a particular element
Description
- after loading the page i am trying to click on a login link
- i expect it to click
- getting the following exception
REST API Reason:java.lang.ClassCastException:
class java.util.LinkedHashMap cannot be cast to class org.openqa.selenium.WebElement
(java.util.LinkedHashMap is in module java.base of loader 'bootstrap';
org.openqa.selenium.WebElement is in unnamed module of loader...
- stacktrace :
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class org.openqa.selenium.WebElement (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; org.openqa.selenium.WebElement is in unnamed module of loader 'app')
at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:166)
at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:59)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:367)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:361)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:194)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:190)
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:627)
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:624)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:203)
Reproducible Code
public void tapLoginButton(WebDriver driver) {
By loginLocator = By.xpath("//p[contains(text(),'Login')]");
safeClick(driver, loginLocator, 10);
}
public void safeClick(WebDriver driver, By locator, int timeoutSeconds) {
WebElement element = waitForElementClickable(driver, locator, timeoutSeconds);
element.click();
}
public WebElement waitForElementClickable(WebDriver driver, By locator, int timeoutSeconds) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds));
return wait.until(ExpectedConditions.elementToBeClickable(locator));
}
Other Information
- Locally does not seem to be happening
- Java version 11
- Observing this randomly on this particular point of our script on trying to interact with this
<p class="D_le M_lK D_lf M_lL D_lj M_lP D_ll M_lR D_lp M_lV D_ls M_lY D_la" style="color:unset">Login</p> - We tried this on browserstack chrome version 138/140 and OS 10 and 11
- sorry, cant share the URL
- We tried without waiting for the element to be clickable by directly clicking on the element, still got the same error
@sjanard, thank you for creating this issue. We will troubleshoot it as soon as we can.
Selenium Triage Team: remember to follow the Triage Guide
⚠️ You reported using Selenium version 4.33, but the latest release is 4.36.
Please verify that this issue still occurs with the latest version. If it no longer applies, you can close this issue or update your comment.
This issue will be marked "awaiting answer" and may be closed automatically if no response is received.
The issue occurs in latest version as well
Hi, @sjanard. Please follow the issue template; we need more information to reproduce the issue.
Either a complete code snippet and URL/HTML (if more than one file is needed, provide a GitHub repo and instructions to run the code), the specific versions used, or a more detailed description to help us understand the issue.
Note: If you cannot share your code and URL/HTML, any complete code snippet and URL/HTML that reproduces the issue is good enough.
Reply to this issue when all information is provided. Thank you.
I think i have added enough details, please let me know if more details are needed
How can we reproduce the issue if we don't have a complete snippet including the website?
And you say it does not happen locally, only on BrowserStack. Then, don't hesitate to contact BrowserStack's customer support.
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class LinkedHashMapBugReproducer {
public static void main(String[] args) throws Exception {
// Setup RemoteWebDriver (BrowserStack or Selenium Grid)
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
// BrowserStack hub URL or your Selenium Grid URL
String hubUrl = "huburl";
RemoteWebDriver driver = new RemoteWebDriver(new URL(hubUrl), caps);
// Navigate to any page
driver.get("https://www.carousell.sg/");
// This call randomly throws ClassCastException when Selenium's
// internal code tries to cast LinkedHashMap to WebElement
// Error: java.lang.ClassCastException: class java.util.LinkedHashMap
// cannot be cast to class org.openqa.selenium.WebElement
// at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:166)
WebElement element = driver.findElement(By.xpath("//p[contains(text(),'Login')]")); // RemoteWebDriver occasionally returns LinkedHashMap instead of WebElement, causing internal ClassCastException
element.click();
driver.quit();
}
}
hope this helps. We checked with browserstack team, but from their side, they said that this looks like a code issue.
An issue in which code?
findElement sometimes returning LinkedHashMap instead of WebElement - so they suspect this could be an issue with selenium.
Does it happen only on that website? Does it happen when you run it locally? What about using Selenium Grid?
Can you share the logs and answer the questions?
I can remember to see this some time ago and could track it to a driver crash, so i did not investigated further. But i think we could try to build a better error message to see what the driver does send.
@diemol what do you think? #16681 is the related PR
@sjanard could you update to 4.39.0 and check the new error message? This should help to better understand the issue