【问题标题】:The type of the expression must be an array type but it resolved to By表达式的类型必须是数组类型,但解析为 By
【发布时间】:2018-08-14 16:58:43
【问题描述】:

你好,我是java新手,Eclipse给我一个错误:

The type of the expression must be an array type but it resolved to By

下面是我的代码:

public boolean currentMonthActivity(){ 
    selectSecondMonth.click(); 
    return Driver.findElements(By.xpath("//div[contains(@class,'ng-binding')]")[1]).size () > 0;
}

【问题讨论】:

  • By.xpath 返回什么?显然它不是一个数组。

标签: java selenium


【解决方案1】:

说明

你写的

By.xpath("//div[contains(@class,'ng-binding')]")[1]

在您的退货声明中。也就是说,您尝试使用[1] 像访问数组一样访问By#xpath 的结果。但是,该方法不返回数组,而是返回By

方法请参考documentation

返回:一个By,它通过XPath定位元素。


解决方案

您可能打算像访问数组一样访问Driver.findElements 的结果。但是,确切的返回类型是List<WebElement>(参见documentation),它不是一个数组。你会像访问它

return Driver.findElements(By.xpath("//div[contains(@class,'ng-binding')]")).get(1);

但是,此访问仅在存在多个元素时才有效(您尝试访问第二个元素)。所以一个安全的变体是:

List<WebElement> elements = Driver.findElements(By.xpath(
    "//div[contains(@class,'ng-binding')]"));

if (elements.size() > 1) {
    return elements.get(1);
} else {
    // Do something different
}

但我不确定这是否正是您想要的,因为您似乎返回了 boolean。请随意更详细地解释它,我会更新解决方案。

【讨论】:

    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多