【发布时间】:2018-03-20 16:21:42
【问题描述】:
我试图在 (http://raspored.finki.ukim.mk/Home/Consultations) 中选择所有带有 tile-consultation 类的 div,单击它们中的每一个并从每个 div 中提取一些数据,我尝试使用:
List<WebElement> professors = driver.findElements(By.className("tile-consultation"));
ListIterator<WebElement> theListOfProfessors = professors.listIterator();
Thread.sleep(1000);
int i = 1;
while(theListOfProfessors.hasNext()) {
WebElement professorI = driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(2)"));
professorI.click();
Thread.sleep(1000);
close = driver.findElement(By.cssSelector("button.btn-close"));
close.click();
Thread.sleep(1000);
}
但是如何在while循环中将1更改为2nd、3d等等?
【问题讨论】:
-
您可能希望使用
findElements(复数)方法,该方法应返回与选择器匹配的所有元素的集合。然后只需从选择器中删除第 n 个类型,它应该都匹配它们。 -
我做了,我将添加其余代码。
-
我不熟悉你使用的语言,但你应该根本不需要
driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(x)"))。你为什么做这个?您应该已经拥有theListOfProfessors中的所有 Web 元素——您应该对其进行迭代,而不是再次尝试检索 Web 元素。 -
我不熟悉Java,但我最好的猜测是这样的(伪代码)
while theListOfProfessors.hasNext(): WebElement elem = theListOfProfessors.next() ...-- 你已经有了listiterator 对象,theListOfProfessors你只需要使用它! -
很高兴听到@M.T——我已经把它变成了一个答案。如果它对您有用,请考虑接受它作为此问题的答案。
标签: java selenium selenium-firefoxdriver