Selenium:将 WebElements 添加到列表,然后检查所有元素的可见性

Selenium: Add WebElements to a List, then check visibility of all of them

使用 Selenium 和 Cucumber,我正在尝试设置一种通过 args 存储 WebElements 的方法。然后使用第二种方法,我试图检查列表中所有元素的可见性。

我有一些元素是这样定位的:

 @FindBy(how = How.XPATH, using = "//* 
 [@id=\"content\"]/section/fieldset/form/div[1]/div[2]/input")
 public WebElement formName;
 @FindBy(how = How.CSS, using = "//* 
 [@id=\"content\"]/section/fieldset/form/div[2]/div[2]/input")
 public WebElement formPassword;
 @FindBy(how = How.ID, using = "remember")
 public WebElement rememberMe;
 @FindBy(how = How.CLASS_NAME, using = "button principal")
 public WebElement loginButton;

然后我写了这个方法来在列表中添加 WebElements:

public void crearListaWebElement(WebElement... elements){
    List<WebElement> webElementList = new ArrayList<>();
    for (WebElement element : elements){
        webElementList.add(elements);
    }
}

在 .add(elements) 1º 问题上出现此错误:

add (java.util.Collection) in List cannot be applied to (org.openqa.selenium.WebElement[])

无法弄清楚为什么我不能将这些相同类型的元素添加到我的列表中

那么,这里是第二种检查可见性的方法:

public void estaVisible(WebDriver(tried WebElement as well) visibleWebElements) {

    boolean esvisible =  driver.findElement(visibleWebElements).isDisplayed();
    Assert.assertTrue(esvisible);
    return esvisible;
}

在 "visibleWebElement" 上出现此错误:

findElement (org.openqa.selenium.By) in WebDriver cannot be applied to (org.openqa.selenium.WebDriver)

我理解这两种不同类型的对象之间的冲突,但是,WebDriver 找到的对象不是存储为 WebElements 吗?

有人可以在这里放点灯吗? 提前致谢。

当您尝试检查 Web 元素列表时,您必须使用以下代码检查所有 elements.Try 的可见性

WebDriver等待网络元素列表

将所有元素存储在一个列表中

List<WebElement> listOfAllWebElements = new ArrayList<WebElement>();
listOfAllWebElements.add(elementOne);
listOfAllWebElements.add(elementTwo);
listOfAllWebElements.add(elementThree);

WebDriverWait listWait = new WebDriverWait(driver,10);
listWait.until(ExpectedConditions.visibilityOfAllElements(listOfAllWebElements));

WebDriverWait for single web element

WebElement element = driver.findElement(By.id("element"));

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(element));

无法弄清楚为什么我不能将这些相同类型的元素添加到我的列表中 -

您的代码中很可能有拼写错误。它应该是 element 而不是 elements :

for (WebElement element : elements){
        webElementList.add(element);
    }

在 "visibleWebElement" -

上出现此错误

driver.findElement() 方法接受一个 By 对象作为参数,它是一个选择器。但是您传递的 visibleWebElements 是一个 WebElement 对象,这就是您的代码出错的原因。比如,如果你想通过xpath定位一个元素,正确的使用方法是:

WebElement your_element = driver.findElement(By.xpath("//whatever xpath"));

您可以直接在 WebElement 上使用 isDisplayed() 方法:

public void estaVisible(WebElement visibleWebElements) {

    boolean esvisible =  visibleWebElements.isDisplayed();
    Assert.assertTrue(esvisible);
    return esvisible;
}

此函数将return一个布尔值并检查列表元素是否可见。

public boolean isListElementsVisible(WebDriver driver, By locator) {
    boolean result = false;
    try {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
        result = true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        result = false;
    }
   return result;
}