如何重写findElements方法,使其返回实现WebElement接口的类型列表



有一个接口WebElement。

我想制作一个实现WebElement的包装器UIElement。

我已经覆盖了所有方法。

但是面临findElementS方法的一个问题,该方法返回List<WebElement>。我已尝试将返回类型更改为List<UI元素>,但代码返回一个错误:">试图使用不兼容的返回类型";。这对我来说很奇怪,因为覆盖findElement方法并指定

UIElement以下是IDE不允许使用的方法,上面有错误:

List<UIElement> list = new ArrayList<>();
for (WebElement el : this.element.findElements(by)) {
UIElement uiElement = new UIElement(this.driver, el);
list.add(uiElement);
}
return list;

以下是UIElement类和构造函数:

public class UIElement implements WebElement {
private final WebDriver driver;
private final WebElement element;
private final Actions actions;
private final JavascriptExecutor jsExecutor;
private final Waiter waiter;

public UIElement(WebDriver driver, By by) {
this.driver = driver;
this.element = this.driver.findElement(by);
this.actions = new Actions(this.driver);
this.jsExecutor = (JavascriptExecutor) this.driver;
this.waiter = new Waiter(this.driver);
}
public UIElement(WebDriver driver, WebElement element) {
this.driver = driver;
this.element = element;
this.actions = new Actions(this.driver);
this.jsExecutor = (JavascriptExecutor) this.driver;
this.waiter = new Waiter(this.driver);
}
}

不可能更改"Webelement列表";至";UI元素列表";在高估的方法中。

不能将List的泛型类型更改为该泛型类型的子类型。问题是,对任何实例的引用都可以是该实例的子类型

有关更多详细信息,请参阅此链接-堆栈溢出

最新更新