Resolved: Watir Scroll For All Comments

In this post, we will see how to resolve Watir Scroll For All Comments


I’m trying to scroll until there are no more contents on a Youtube video. I’m testing a counter versus the current number of comments displayed.
After the first pagedown, the count of comments should increase. It isn’t. I’ve entered a sleep 10 for the comments to load and the count of those comments to also update. It doesn’t update. I keep getting 20 : 20 so it breaks and leaves this iteration after a single iteration.
I’m not sure why that valuation isn’t updating. How can I fix this so that it can get to the end of the comments?

Best Answer:

I ran into 2 problems when running your script:
  1. browser.div(id: "contents").divs(id: "comment-content") returned nothing. There were many “contents” divs, with the first one not including any comments. I removed this locator.
  2. Scrolling by 1000 is not enough to get to the bottom of the page. Therefore the loading of additional comments does not get triggered.

From the following script:
You can see that having a larger scroll gives the expected results:
In contrast, setting the scroll_by to just “1000” did not trigger the comments. The output was just:

If you have better answer, please add a comment about this, thank you!