【问题标题】:How do I click the next button in pagination, where the next button doesn't have a class?如何在分页中单击下一个按钮,下一个按钮没有类?
【发布时间】:2021-06-12 03:44:02
【问题描述】:

我在使用 selenium 网络驱动程序从网站上抓取所有内容时遇到了一些麻烦。我遇到的问题是分页上的下一个按钮没有类,这是分页部分的 html:

<div>
     <ul class="pagination">
         <li class="disabled>
              <a href="#">
                  <span>« </span>
                  First
              </a>
         </li>
         <li class="disabled">
              <a href="#">
                  <span>‹ </span>
                  Previous
              </a>
         </li>
         <li class="disabled">
              <a href="#">
                  1
              </a>
         </li>
         <li class="">
              <a href="#" rel="next">
                  2
              </a>
         </li>
         <li class="">
             <a href="#">
                 3
             </a>
         </li>
         #........ same again for pages 4 and 5
         <li class="">
             <a href="#" rel="next">
                 Next
                 <span> ›</span>
             </a>
         </li>
         <li class="">
             <a href="#">
                 Last
                 <span> »</span>
             </a>
         </li>

这是我用来单击具有“分页”类的元素的代码:

while True:

# get data from page here

# click next page
    try:
        element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'pagination')))
        element.click()
    except TimeoutException:
        # no pages left
        break

我遇到的问题是它只识别分页中的第一个可点击按钮,这意味着它在点击第 2 页(启用“第一个”按钮)之间交替,然后点击第一个按钮并切换返回第 1 页。有没有办法可以进一步缩小可点击元素的范围,使其只点击下一个按钮?

【问题讨论】:

    标签: python selenium pagination webdriver


    【解决方案1】:

    尝试等待next按钮:

    element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.pagination a[rel=next]'))
    

    【讨论】:

    • 效果很好,非常感谢!
    猜你喜欢
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多