gpt4 book ai didi

java.lang.ClassCastException : In Web-DriverWait when using PageFactory with POM

转载 作者:行者123 更新时间:2023-12-02 13:28:08 24 4
gpt4 key购买 nike

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

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

    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。你不需要那里的 Actor 。更好的是,您应该等待该元素可单击后再单击它。我会像下面这样重写它。

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

此外,Selenium 的创建者 Simon Stewart,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();
}

关于java.lang.ClassCastException : In Web-DriverWait when using PageFactory with POM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43314243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com