根据元素定位到所有课程名称并放到 list 集合: List<webElement> courseList = driver.findElements(By.className("shizan-name"));
- List<WebElement> courseList = driver.findElements(By.className("shizan-name"));
- for (int i = 0;i<courseList.size();i++) {
- courseList.get(i).click();// 根据下标获取到课程名
- driver.navigate().back();// 返回到上一页
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- driver.findElement(By.className("js-close")).click();// 关闭浮窗
- // 返回之后页面刷新了, 所以要给 courseList 重新赋值, 再次进行循环才能正确定位到元素
- courseList = driver.findElements(By.className("shizan-name"));
来源: http://www.bubuko.com/infodetail-3465053.html