首页 > 解决方案 > 网页抓取 / 无法点击下一页

问题描述

我想获得下一个网站,例如。在这个网站上: https ://www.11880.com/suche/Naturpark/deutschland

<div class="next">
   <form class="link-form" action="https://www.11880.com/form" method="POST">
        <input type="hidden" name="source" value="*JSPPyTNQ4Hl4n6FJKGEcgCWqLzgK2zccsymE3agiLSMAKNMw9kRza81Id4CrUpZ08MJMZZgtfLcy7UQJ5Y8LxQ">
        <button class="link icon-right" title="Zur nächsten Seite" data-page="1" type="submit">
        </button>
   </form>
</div>

我尝试使用以下语句单击元素:

driver.find_element_by_xpath ('//*[@id="searchresultlist"]/div/div[1]/div[4]').click()
driver.find_elements_by_class_name("link icon-right").click()
driver.find_element_by_css_selector("[title^='4G Signal quality']")

在第一行我得到这个错误:

selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素 ... 在点 (861、949) 处不可点击。其他元素会收到点击:

...

使用第二行作为尝试,我得到了这个错误:(显然更多的 1 元素与类“link icon-right”一起找到)

Traceback (most recent call last):
  File "c:\Users\Polzi\Documents\DEV\Fiverr\papillion\search11880.py", line 80, in <module>
    driver.find_elements_by_class_name("link icon-right").click()
AttributeError: 'list' object has no attribute 'click'

并尝试使用第三行,我收到此错误:

selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素在点(566、919)不可点击。其他元素会收到点击:...(会话信息:chrome=91.0.4472.106)

Any ideas how i can get this page right element clicked?

标签: python-3.xseleniumweb-scrapingbeautifulsoup

解决方案


“下一页”按钮出现在网页底部。在单击它之前,您必须滚动到该元素。
如下所示:

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)

next_btn = driver.find_element_by_css_selector(".next")
actions.move_to_element(next_btn).perform()
next_btn.click()

UPD
如果单击“下一步按钮”被 cookie-bar 元素拦截,您可以在单击下一页按钮之前将其关闭

driver.find_element_by_xpath('//button[@class="btn btn-close-cookiebar"]').click()

或者您可以使用 JavaScript 单击“下一页按钮”,而不是使用驱动程序单击它,如下所示:

driver.execute_script("arguments[0].click();", next_btn)

推荐阅读