I have a selenium webdriver script which performs some regression tests on my application under test. The script works perfectly on Google Chrome, Firefox, IE, etc.
This error message...
org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {}
...implies that WebDriverException was raised while JVM tried to cast the returned value into a WebElement.
However your main issue is as follows:
java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
ClassCastException is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. As an example, the following code generates a ClassCastException:
Object x = new Integer(0);
System.out.println((String)x);
It is not clear about your usecase why you require to grab the <body>
tag. But as per the following discussions:
There can be three possibilities of this error as follows:
<body>
tag when the page was still loading perhaps when some JavaScript / Ajax was still active. Solution: Induce WebDriverWait for the WebElement to whom you desire to interact with as follows:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("elementID")));
If you want to get the Page Source use getPageSource()
method as follows:
System.out.println(driver.getPageSource());
If the control of the program was within an <iframe>
before trying to find the <body>
tag, switch back to the defaultContent as follows:
driver.switchTo().defaultContent();
Note: As per best practices always keep your Test Environment updated with the latest releases.
Update the Selenium Client dependency to 3.12.0:
selenium-java
:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.12.0</version>
</dependency>
selenium-server
:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.12.0</version>
</dependency>