[英]java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement when executing test through Selenium
I'm trying make an automation test in Selenium WebDriver, but Eclipse is showing me the following error: 我正在尝试在Selenium WebDriver中进行自动化测试,但是Eclipse向我显示了以下错误:
java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement
Here is my code: 这是我的代码:
public void SeachApp()
{
Object elem = driver.getPageSource().contains("Staffing");
if (elem != null)
{
System.out.println("Encontrado");
int width = ((WebElement) elem).getSize().getWidth();
Actions act = new Actions(driver);
act.moveToElement((WebElement) elem)
.moveByOffset((width / 2) - 15, 0)
.click()
.perform();
}
else
{
System.out.println("Não Encontrado");
}
}
What's going on, and how can I fix this? 这是怎么回事,我该如何解决?
As per the documentation getPageSource()
returns the source of the current page and is defined as: 根据文档,
getPageSource()
返回当前页面的源,并定义为:
Get the source of the last loaded page.
获取上次加载页面的来源。 If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page.
如果在加载后修改了页面(例如,通过Javascript),则不能保证返回的文本是修改后的页面的文本。 You need to follow the documentation of the particular driver being used to determine whether the returned text reflects the current state of the page or the text last sent by the web server.
您需要遵循用于确定返回的文本是反映页面的当前状态还是Web服务器最后发送的文本的特定驱动程序的文档。 The page source returned is a representation of the underlying DOM: do not expect it to be formatted or escaped in the same way as the response sent from the web server.
返回的页面源是底层DOM的表示形式:不要期望它以与从Web服务器发送的响应相同的方式进行格式化或转义。
The contains()
method is a Java method to check if the String contains another substring or not. contains()
方法是一种Java方法,用于检查String是否包含另一个子字符串 。 It returns boolean value so it can use directly inside if statements. 它返回布尔值,因此可以直接在if语句内部使用。
The expression which you have used: 您使用的表达式:
driver.getPageSource().contains("Staffing");
will return a Boolean Value but gets casted into an Object
type of object. 将返回布尔值,但将其转换为
Object
类型。 Further, as you are trying to cast an Object
type of object to WebElement
type you see the error as: 此外,当您尝试将
Object
类型转换为WebElement
类型时,您会看到以下错误:
java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement
If you desire to locate a WebElement you have to use either of the following methods: 如果要定位WebElement ,则必须使用以下两种方法之一:
Now, if you desire to get hold of the WebElement with text as Staffing (without any reference to the relevant HTML ) you can use the following solution: 现在,如果您希望获得文本为Staffing的WebElement (无需任何对相关HTML的引用),则可以使用以下解决方案:
WebElement element = driver.findElement(By.xpath("//*[contains(text(),'Staffing')]"));
Now, once you located the WebElement , you can easily perform the remaining of your tasks with the WebElement . 现在,一旦你所在的WebElement,你可以很容易地与WebElement执行你的任务剩余。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.