简体   繁体   English

Python Selenium单击下一步按钮直到结束

[英]Python Selenium clicking next button until the end

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

This is the html tag from the website with the button: 这是带有按钮的网站html标记:

<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>

I've tried this code: 我已经试过这段代码:

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. 而且我也检查了这个问题 ,但仍然无法解决。

Any thoughts? 有什么想法吗?

pager results-display are actually two classes that belongs to the button's parent element. pager results-display实际上是属于按钮的父元素的两个类。 They aren't name attribute, and find_element_by_name gets only one name anyway. 它们不是name属性,无论如何, find_element_by_name仅获得一个名称。 Try 尝试

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

Notice that you might have to relocate the button each time if the DOM has changed after the click. 请注意,如果单击后DOM发生了更改,则每次可能必须重新定位按钮。

To click in loop as long as the button is active you can check if the buuton has class inactive 要在按钮处于活动状态时单击循环,您可以检查按钮是否处于inactive

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

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

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