网页抓取/下一页无法点击

时间:2021-06-20 17:33:31

标签: python-3.x selenium web-scraping beautifulsoup

我想获取下一个站点,例如。在这个网站上: 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) 处可点击。其他元素会收到点击:

...

尝试使用第二行时出现此错误: (显然更多的是在“link icon-right”类中找到的 1 个元素)

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?

1 个答案:

答案 0 :(得分:1)

“下一页”按钮出现在网页底部。您必须先滚动到该元素,然后再单击它。
像下面这样:

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)