【问题标题】:Watir wait for more posts loadWatir 等待更多帖子加载
【发布时间】:2019-08-19 12:54:49
【问题描述】:

我要抓取的网站正在通过滚动事件进行延迟加载。 (无限滚动) 我想等待更多帖子加载,但所有帖子都有相同的类名。所以,我等不及要加载具有特定类名的元素了。我不想使用sleep() 方法。

示例代码:

<div class='posts'>
 <div class='single-post'></div>
 <div class='single-post'></div>
 <div class='single-post'></div>
 <div class='single-post'></div>
 // The following posts appear after scrolling. I want to fetch these posts
 <div class='single-post'></div>
 <div class='single-post'></div>
 <div class='single-post'></div>
 <div class='single-post'></div>
</div>

我该如何解决这个问题?是否可以检查此 div 是否有超过 10 个帖子?

想法?

【问题讨论】:

    标签: ruby-on-rails ruby watir


    【解决方案1】:

    您可以获取单个帖子 div 的集合,以查看是否加载了更多。根据页面的标记,您可以:

    browser.divs(class: 'single-post').count
    

    或者,如果父母很重要:

    browser.div(class: 'posts').divs(class: 'single-post').count
    

    这个可以用来等待发帖数增加:

    # Find the original count
    original_count = browser.divs(class: 'single-post').count
    
    # Take an whatever action to trigger the loading of more posts
    browser.scroll.to :bottom
    
    # Wait for the count to increase
    browser.wait_until do
      new_count = browser.divs(class: 'single-post').count
      new_count > original_count
    end
    

    【讨论】:

    • 谢谢你,干得好..我也解决了这个问题:Watir::Wait.until { browser.div(class: 'posts').div(index: 25).exist? } 索引号由你决定
    • @OğuzÇiçek 我喜欢你的方法,如果你走这条路,你可以用更少的代码轻松地做到这一点:browser.div(class: 'posts').div(index: 25).wait_until(&amp;:exist?)
    • 你甚至可以缩短到browser.div(class: 'single-post', index: 25).wait_until(&amp;:exists?)
    猜你喜欢
    • 2018-03-15
    • 1970-01-01
    • 2023-03-04
    • 2018-03-07
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多