Selenium select 每个 div 分别具有相同的 class
Selenium select each div separately that have the same class
我正在尝试 select (http://raspored.finki.ukim.mk/Home/Consultations) 中的所有 divwithclass tile-consultation,单击它们中的每一个并从每个 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 等等?
您已经完成了工作。您已经在此处找到了 Web 元素并创建了一个列表迭代器:
List<WebElement> professors = driver.findElements(By.className("tile-consultation"));
ListIterator<WebElement> theListOfProfessors = professors.listIterator();
findElements
方法将return 匹配选择器的元素集合。您无需像在该循环内尝试使用 driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(x)"
那样再次检索元素。您只需要迭代您已经创建的列表迭代器 theListOfProfessors
。例如。效果
while(theListOfProfessors.hasNext()) {
WebElement elem = theListOfProfessors.next()
// do something with elem
}
我正在尝试 select (http://raspored.finki.ukim.mk/Home/Consultations) 中的所有 divwithclass tile-consultation,单击它们中的每一个并从每个 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 等等?
您已经完成了工作。您已经在此处找到了 Web 元素并创建了一个列表迭代器:
List<WebElement> professors = driver.findElements(By.className("tile-consultation"));
ListIterator<WebElement> theListOfProfessors = professors.listIterator();
findElements
方法将return 匹配选择器的元素集合。您无需像在该循环内尝试使用 driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(x)"
那样再次检索元素。您只需要迭代您已经创建的列表迭代器 theListOfProfessors
。例如。效果
while(theListOfProfessors.hasNext()) {
WebElement elem = theListOfProfessors.next()
// do something with elem
}