首页 > 解决方案 > WebElementProxy 应该实现什么才能将其传递给 Actions.moveToElement(WebElement)?

问题描述

里面有org.openqa.selenium.interactions.Actions一个名为的方法moveToElement(WebElement target);

我有一个类WebElementProxy,它实现WebElement

public class WebElementProxy implements WebElement {
    private WebElement el;
    public WebElementProxy(WebElement el) {
        this.el = el;
    }
    /* Implement methods from WebElement, delegating directly to el field. */
}

当我尝试打电话时,new Actions(driver).moveToElement(webElementProxy)我收到以下错误:

class mypackage$WebElementProxy cannot be cast to class org.openqa.selenium.interactions.Locatable

因此,我在 WebElementProxy 的接口中添加了 Locatable:

public class WebElementProxy implements WebElement, Locatable {
    private WebElement el;
    public WebElementProxy(WebElement el) {
        this.el = el;
    }
    /* Implement methods from WebElement and Locatable, delegating directly to el field. */
}

现在,当我打电话时,new Actions(driver).moveToElement(webElementProxy)我收到以下错误:

invalid argument
from invalid argument: 'origin' must be either a string or a dictionary
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:618)
    at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:639)
    at org.openqa.selenium.interactions.Actions.perform(Actions.java:595)
    at the place in my project where I called actions.movetoElement()

在这一点上,我不知道我还需要实现什么才能使这个方法调用正常工作。我在这里想念什么?

标签: javaseleniumselenium-webdriver

解决方案


在类似的情况下(使用 Serenity 的 WebElementFacade),它通过直接传递底层元素为我解决了:new Actions(driver).moveToElement(el).


推荐阅读