Python Selenium单击下一步按钮直到结束 [英] Python Selenium clicking next button until the end

查看:101
本文介绍了Python Selenium单击下一步按钮直到结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题的后续问题,我正在尝试抓取网站并让Selenium单击next(直到无法单击为止)并收集结果.

This is a follow up question from my first question, and I am trying to scrape a website and have Selenium click on next (until it cant be clicked) and collect the results as well.

这是带有按钮的网站html标记:

This is the html tag from the website with the button:

<div class="pager results-display">
  <span class="action-btn prev inactive" data-page="1">Prev</span>
  <span class="action-btn inactive" data-page="1">1</span>  # Currently in page 1 thus inactive
  <span class="action-btn" data-page="2">2</span>
  <span class="action-btn" data-page="3">3</span> 
  <span class="action-btn" data-page="4">4</span>
  <span class="action-btn" data-page="5">5</span>
  <span class="action-btn" data-page="6">6</span>
  <span class="action-btn" data-page="7">7</span>
  <span class="action-btn" data-page="8">8</span> 
  <span class="action-btn" data-page="9">9</span>
  <span class="action-btn" data-page="10">10</span>
  <span class="action-btn" data-page="11">11</span> 
  <span class="action-btn" data-page="12">12</span>
  <span class="action-btn" data-page="13">13</span>
  <span class="action-btn" data-page="14">14</span>
  <span class="action-btn next" data-page="2">Next</span>
</div>
<div class="no-results-display hidden" style="display: none;">
                    No companies matched your search. Try again.
                </div>

我尝试了以下代码:

elm = driver.find_element_by_name('pager results-display')
elm.click()

我还检查了此

And I've also checked this question out, but still wasn't able to solve it.

有什么想法吗?

推荐答案

pager results-display实际上是两个属于按钮的父元素的类.它们不是名称属性,并且find_element_by_name无论如何都只能获得一个名称.试试

pager results-display are actually two classes that belongs to the button's parent element. They aren't name attribute, and find_element_by_name gets only one name anyway. Try

elm = driver.find_element_by_class_name('next')
elm.click()

请注意,如果单击后DOM发生了变化,则每次都可能需要重新定位按钮.

Notice that you might have to relocate the button each time if the DOM has changed after the click.

要在按钮处于活动状态时单击循环,您可以检查按钮是否具有类inactive

To click in loop as long as the button is active you can check if the buuton has class inactive

while True:
    elm = driver.find_element_by_class_name('next')
    if 'inactive' in elm.get_attribute('class'):
        break;
    elm.click()

这篇关于Python Selenium单击下一步按钮直到结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆