2017-04-10 31 views
0

我在Selenium中使用Page工厂模型来初始化Web元素。 我在我的代码中有等待操作,同时将web元素传递给我的等待操作,它抛出“ClassCastException”。 我无法找到解决方案,任何线索都会很好。请建议我一些方法将Page工厂的对象转换为WebElement对象。java.lang.ClassCastException:在Web-DriverWait中使用PageFactory和POM时

@FindBy(how = How.XPATH, using = "//*[@id='menu-posts']/div[3]/div/ul/li[3]/a") public WebElement categories;

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) 
+0

什么是元素? –

+0

你需要改变'wait.until(ExpectedConditions.visibilityOfElementLocated((By)element));'to'wait.until(ExpectedConditions.visibilityOf(element));' – kushal

+0

非常感谢。它有帮助。 –

回答

1

您是铸造变量categoriesWebElement时,它已经是一个WebElement

改变这一行

waitClick((WebElement) categories); 

这个

waitClick(categories); 

,它应该摆脱异常。

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

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

更进一步,硒的创建者,西蒙斯图尔特recommends not using Page Factory。我会将定位器存储在类的顶部,然后根据需要使用它们。我会重写整个事情如下。

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(); 
} 
相关问题