问题
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.
However, recently I tried running it on Safari (11.1.1) on Mac OS X (10.13.5), and my script fails with a weird message even when I call a simple line such as
driver.findElement(By.tagName("body"));
The exception I get is as follows:
org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {}
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'Yethis-MacBook-Pro.local', ip: '192.168.2.197', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.5', java.version: '1.8.0_171'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:375)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByTagName(RemoteWebDriver.java:441)
at org.openqa.selenium.By$ByTagName.findElement(By.java:334)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
at org.ycs.selenium.safari.App.execute(App.java:75)
at org.ycs.selenium.safari.App.main(App.java:27)
Caused by: java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:373)
... 5 more
Selenium Version: 3.0.1 Safari Version: 11.1.1 Java Version: 1.8 OS X version: 10.13.5
Edit: This is my dependency configured in maven
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.0.1</version>
</dependency>
I tried doing a lot of search for this issue, but unfortunately, nothing showed up with Mac specific information.
Could anyone help me solve this problem please?
Thanks, Sriram Sridharan
回答1:
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
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);
What went wrong
It is not clear about your usecase why you require to grab the <body>
tag. But as per the following discussions:
- java.lang.ClassCastException exception while clicking/inputing on any web object
- WebDriver and Firefox 4+: this.getWindow() is null
There can be three possibilities of this error as follows:
- Your script/program was trying to access the
<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>
来源:https://stackoverflow.com/questions/51020225/webdriverexception-returned-value-cannot-be-converted-to-webelement-while-u