使用 Selenium 和 Cucumber,我尝试设置一种通过参数存储 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 元素列表时,您必须检查所有元素的可见性。尝试使用以下代码
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 单个 Web 元素
WebElement element = driver.findElement(By.id("element"));
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(element));
关于java - Selenium:将 WebElement 添加到列表中,然后检查所有元素的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51398868/