简体   繁体   English

java.lang.ClassCastException:通过Selenium执行测试时,无法将java.base / java.lang.String强制转换为org.openqa.selenium.WebElement

[英]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? 这是怎么回事,我该如何解决?

getPageSource() getPageSource()

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服务器发送的响应相同的方式进行格式化或转义。


contains() 包括()

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语句内部使用。


Analysis 分析

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

Solution

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: 现在,如果您希望获得文本为StaffingWebElement (无需任何对相关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.

相关问题 java.lang.ClassCastException:org.openqa.selenium.By $ ById无法转换为org.openqa.selenium.WebElement - java.lang.ClassCastException: org.openqa.selenium.By$ById cannot be cast to org.openqa.selenium.WebElement java.lang.ClassCastException: 类 org.openqa.selenium.By$ByXPath 不能转换为类 org.openqa.selenium.WebElement - java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class org.openqa.selenium.WebElement 线程“主”中的异常java.lang.ClassCastException:无法将java.util.ArrayList强制转换为org.openqa.selenium.WebElement - Exception in thread “main” java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement 线程“ main”中的异常java.lang.ClassCastException:java.util.HashMap无法转换为org.openqa.selenium.WebElement - Exception in thread “main” java.lang.ClassCastException: java.util.HashMap cannot be cast to org.openqa.selenium.WebElement java.lang.ClassCastException:无法转换为 org.openqa.selenium.WebElement 使用 executeScript() 从 shadowHost 返回 shadowRoot - java.lang.ClassCastException: cannot be cast to org.openqa.selenium.WebElement using executeScript() to return shadowRoot from shadowHost java.lang.ClassCastException:类 java.lang.String 不能强制转换为类 [B(java.lang.String 和 [B 在加载程序'bootstrap 的模块 java.base 中) - java.lang.ClassCastException: class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader 'bootstrap 不兼容的类型:java.lang.Object无法转换为org.openqa.selenium.WebElement - incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.WebElement java.lang.ClassCastException: org.openqa.selenium.firefox.FirefoxDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen - java.lang.ClassCastException: org.openqa.selenium.firefox.FirefoxDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen java.lang.ClassCastException:无法将java.lang.String强制转换为org.myapp.ui.MyClass - java.lang.ClassCastException: java.lang.String cannot be cast to org.myapp.ui.MyClass java.lang.ClassCastException:org.apache.camel.builder.ValueBuilder无法转换为java.lang.String - java.lang.ClassCastException: org.apache.camel.builder.ValueBuilder cannot be cast to java.lang.String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM