我试图循环for循环7次,其中webelement会点击只有7次。即使网页中还有其他commentsbutton元素,循环也会忽略它们并停止点击。for循环的点击网页元素
目前,该脚本将仍然点击webelement不停。
WebElement commentsbutton = (WebElement) comment.findElement(By.xpath("//button[contains(@class,'-cx-PRIVATE-PostInfo__commentsLoadMoreButton -cx-PRIVATE-PostInfo__commentsLoadMoreButtonEnabled')]")); //view all/ load more comments
//click more comments
for(int i = 0; i < 7; i++) {
//for (WebElement comments_element : commentsbutton) {
commentsbutton.click(); //click on button if found
Thread.sleep(3000); //pause for 5 seconds
System.out.println(commentsbutton);
//}
}
你必须找到下一个元素每次迭代? –
@AndrewPiliser我只需要找到这个相同的元素7次,并忽略其他人如果有超过7个这些元素。 –
当页面打开时,您的页面是否包含7个或更多commentsButton?或者在点击第一个评论按钮后出现第二个评论按钮? – Sighil