java - 更改方法findByOneCss,他收到了不止一种含义和所有

标签 java selenium-webdriver css-selectors

这是方法本身

private static WebElement findByOneCss (WebElement we, String selector) {
    try {
        return we.findElement (By.cssSelector (selector));
    } catch (NoSuchElementException e) {
        return null;
    }
}

我知道您需要更改为 findElement findElements 但不考虑所有 View 。

我有一个循环绕过他这里的值

for (WebElement wle: wlElements) {
    WebElement commenWe = findByOneCss (wle, ".activity-new-val");
    wl.setReason1 (commenWe! = null? commenWe.getText (): "");
    wlList.add (wl);
}

它需要改变什么,我试着喜欢这个

List <WebElement> commenWe = findAllByOneCss (wle, ".activity-old-val");
wl.setReason (commenWe! = null? ((WebElement) commenWe) .getText (): "");
wlList.add (wl);

但它克服了 java.util.ArrayList 无法转换为 org.openqa.selenium.WebElement 的错误

最佳答案

根据 wlList 的类型,您可以使用:wlList.addAll(commenWe);

或者你迭代commenWe喜欢这样:

    List<WebElement> commenWe = findAllByOneCss(wle, ".activity-old-val");
    for (WebElement we : commenWe) {
        //whatever [wl] is...
        wl.setReason(we!=null ? ((WebElement) we).getText(): "");
        wlList.add(wl);
    }

关于java - 更改方法findByOneCss,他收到了不止一种含义和所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32819445/

相关文章:

java - 显式等待 X 元素可见

selenium - headless 谷歌浏览器 : How to prevent sites to know whether their window is focused or not

Selenium WebDriver 或 Selenium RC 的 xpath 和 cssSelector

CSS 不是 :last-child or last-child not selecting as expected

java - 使用 Hibernate 二级缓存和 Multi-Tenancy

java - 使用正则表达式将缩写重新组合在一起

java - 如何计算浮点值并将其拆分为两个单独的整数值?例如 18.5f 小时等于 int h=18 int m=30

java - 仅当指定端口时,JNLP 应用程序才会被安全设置阻止

javascript - Selenium 网络驱动程序 : Element is not currently visible

html - 多个 :selectors on one CSS element?