简体   繁体   English

用于单击 Web 元素的 For 循环

[英]For-loop for clicking web element

I'm trying to loop the for loop 7 times where the webelement will be click 7 times only.我试图循环 for 循环 7 次,其中 webelement 将仅单击 7 次。 Even if there are other commentsbutton element in the web page, the loop will ignore them and stop clicking.即使网页中有其他commentsbutton元素,循环也会忽略它们并停止点击。

Currently, the script will still click on the webelement non-stop.目前,脚本仍然会不停地点击 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);
    //}
}

Try this:尝试这个:

WebDriverWait wait = new WebDriverWait(driver, 10);

for(int i=0; i<7; i++) {
    WebElement element = wait.until(
       ExpectedConditions.visibilityOfElementLocated(
           By.xpath("//button[@class='-cx-PRIVATE-PostInfo__commentsLoadMoreButtonEnabled']")));

     driver.findElement(By.xpath("//button[@class='-cx-PRIVATE-PostInfo__commentsLoadMoreButtonEnabled']")).click();
}

Explanation: As per the comments below, you mentioned that you actually wanted to click on the load more comments button on a Instagram post for exactly 7 times.说明:根据下面的评论,您提到您实际上想要点击 Instagram帖子上的load more comments按钮 7 次。 So, if you observe the HTML of the page, especially that of the button, is:因此,如果您观察页面的 HTML,尤其是按钮的 HTML,则是:

<button class="-cx-PRIVATE-PostInfo__commentsLoadMoreButton -cx-PRIVATE-PostInfo__commentsLoadMoreButtonEnabled">

But once you click the button, the site start fetching more comments to display.但是一旦您单击该按钮,该站点就会开始获取更多要显示的评论。 In that period, the class name value -cx-PRIVATE-PostInfo__commentsLoadMoreButtonEnabled is removed the HTML becomes this:在那个时期,类名值-cx-PRIVATE-PostInfo__commentsLoadMoreButtonEnabled被删除,HTML 变成这样:

<button class="-cx-PRIVATE-PostInfo__commentsLoadMoreButton" disabled="true">

Once it fetches the data and displays it, the class name value which was removed is added back and the button gets enabled.一旦它获取数据并显示它,被删除的类名值就会被添加回来并且按钮被启用。

Hence, if you want to click on the button multiple times to load all or most of the comments, you are gonna have to use a combination of waits and loops.因此,如果您想多次单击按钮以加载所有或大部分评论,您将不得不使用等待和循环的组合。 You were using Thread.sleep() to give a pause and then click on the button.您正在使用Thread.sleep()暂停,然后单击按钮。 But then using a wait is a better option, since it polls the DOM to find the element for that period of time and once it meets it expected condition (in this case visibility of the element), it then moves on to execute the next line of code.但是使用等待是一个更好的选择,因为它轮询 DOM 以查找该时间段内的元素,一旦它满足预期条件(在这种情况下元素的可见性),它就会继续执行下一行的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM