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

使用 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º 问题上出现此错误:


添加 (java.util.Collection) in List 不能应用于 (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”上收到此错误:


WebDriver 中的 findElement (org.openqa.selenium.By) 不能应用于 (org.openqa.selenium.WebDriver)


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


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


人到中年有点甜
浏览 445回答 3
3回答

斯蒂芬大帝

无法弄清楚为什么我不能将这些相同类型的元素添加到我的列表中 -您的代码中很可能有拼写错误。它应该是element而不是elements:for (WebElement element : elements){&nbsp; &nbsp; &nbsp; &nbsp; webElementList.add(element);&nbsp; &nbsp; }在“visibleWebElement”上出现此错误 -driver.findElement()方法接受一个By对象作为参数,它是一个选择器。但是您正在传递visibleWebElementswhich 是一个WebElement对象,这就是您的代码出错的原因。例如,如果您想通过 xpath 定位元素,这是使用它的正确方法:WebElement your_element = driver.findElement(By.xpath("//whatever xpath"));您可以直接isDisplayed()在 WebElement 上使用该方法:public void estaVisible(WebElement visibleWebElements) {&nbsp; &nbsp; boolean esvisible =&nbsp; visibleWebElements.isDisplayed();&nbsp; &nbsp; Assert.assertTrue(esvisible);&nbsp; &nbsp; return esvisible;}

慕雪6442864

此函数将返回一个布尔值并检查列表元素是否可见。public boolean isListElementsVisible(WebDriver driver, By locator) {&nbsp; &nbsp; boolean result = false;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; WebDriverWait wait = new WebDriverWait(driver, 30);&nbsp; &nbsp; &nbsp; &nbsp; wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));&nbsp; &nbsp; &nbsp; &nbsp; result = true;&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; result = false;&nbsp; &nbsp; }&nbsp; &nbsp;return result;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java