Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.ClassCastException:在Web驱动程序中将PageFactory与POM一起使用时使用_Java_Selenium_Selenium Webdriver_Selenium Rc - Fatal编程技术网

java.lang.ClassCastException:在Web驱动程序中将PageFactory与POM一起使用时使用

java.lang.ClassCastException:在Web驱动程序中将PageFactory与POM一起使用时使用,java,selenium,selenium-webdriver,selenium-rc,Java,Selenium,Selenium Webdriver,Selenium Rc,我使用selenium中的页面工厂模型初始化web元素。 我的代码中有等待操作,当将web元素传递给我的等待操作时,它抛出“ClassCastException”。 我无法找到解决方案,任何线索都会很好。请建议我一些方法来强制转换页面工厂“对象到WebElement”对象 @FindBy(how=how.XPATH,使用=“//*[@id='menu-posts']]/div[3]/div/ul/li[3]/a”) 公共网页元素类别 public void menus() {

我使用selenium中的页面工厂模型初始化web元素。 我的代码中有等待操作,当将web元素传递给我的等待操作时,它抛出“ClassCastException”。 我无法找到解决方案,任何线索都会很好。请建议我一些方法来强制转换页面工厂“对象到WebElement”对象

@FindBy(how=how.XPATH,使用=“//*[@id='menu-posts']]/div[3]/div/ul/li[3]/a”)
公共网页元素类别

    public void menus() {
            try {
                loginTest();
                menuPosts.click();
                waitClick((WebElement) categories);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                driver.quit();
            }
        }
    public void waitClick(WebElement element) {
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOfElementLocated((By) element));
        element.click();
    }


**Exception trace:**

    java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to org.openqa.selenium.By
        at com.pageObject.categories.waitClick(categories.java:75)
        at com.pageObject.categories.menus(categories.java:54)
        at SeleniumFramework.com.framework.AppTest.viewPost(AppTest.java:37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

当变量
categories
已经是
WebElement
时,您正在将其强制转换为
WebElement

换行

waitClick((WebElement) categories);
对此

waitClick(categories);
它应该消除这个异常

您将遇到的另一个问题是
waitClick()
函数正在将
WebElement
转换为
By
。你不需要演员。更好的是,在单击元素之前,应该等待该元素可单击。我会像下面那样重写它

public void waitClick(WebElement element)
{
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element)).click();
}
此外,硒的创造者西蒙·斯图尔特。相反,我会将定位器存储在类的顶部,然后根据需要使用它们。我会把整件事重写如下

public By categoriesLocator = By.xpath("//*[@id='menu-posts']/div[3]/div/ul/li[3]/a");

public void menus()
{
    try
    {
        loginTest();
        menuPosts.click(); // why aren't you using waitClick() here?
        waitClick(categoriesLocator);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        driver.quit();
    }
}

public void waitClick(By locator)
{
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(locator)).click();
}

什么是元素?您需要更改
wait.until(ExpectedConditions.visibilityOfElementLocated((By)element))
等待.直到(元素的预期条件.可视性))非常感谢。这有帮助。