更新时间:2023-01-13 11:05:44
此错误消息...
org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {}
...表示在 JVM 尝试将返回的值转换为 WebElement 时引发了WebDriverException.
...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:
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);
不清楚您的用例为何需要获取<body>
标记.但是按照以下讨论:
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>
标记. 解决方案:为要与之交互的 WebElement 生成 WebDriverWait ,如下所示:
<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")));
如果要获取页面源,请使用 getPageSource()
方法,如下所示:
If you want to get the Page Source use getPageSource()
method as follows:
System.out.println(driver.getPageSource());
如果在尝试找到<body>
标记之前该程序的控件位于<iframe>
之内,请切换回 defaultContent ,如下所示:
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.
将 Selenium Client 依赖项更新为 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>