点击“更多"通过硒按钮 [英] Clicking "More" button via selenium

查看:63
本文介绍了点击“更多"通过硒按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取以下网站: https://angel.co/companies

I am trying to scrape the following website : https://angel.co/companies

底部有一个更多"按钮,单击该按钮可以加载更多记录.

There is a "More" button at the bottom, which on click loads more records.

我需要通过硒单击按钮.

I need to click the button via selenium.

我尝试了以下操作:

python_button = driver.find_elements_by_class_name("more")
python_button.click()

但是找不到合适的按钮,即我的python_button返回一个空列表.

However its not finding the appropriate button, i.e. my python_button returns an empty list.

我尝试了以下操作:

python_button = driver.find_element_by_class_name("more")

这会导致以下错误:

消息:否这样的元素:无法找到元素:{"method":"class 名称",选择器":更多"}

Message: no such element: Unable to locate element: {"method":"class name","selector":"more"}

有什么想法可以解决这个问题吗?

Any ideas to get around this?

推荐答案

您将点击更多按钮的次数越多,将加载的数据就越多.您需要为按钮上的更多 文本诱导 WebDriverWait 以便单击,并且可以使用以下解决方案:

The more you will click the MORE button more data will be loaded. You need to induce WebDriverWait for the button with text with MORE to be clickable and you can use the following solution:

  • 代码块:

  • Code Block:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://angel.co/companies")
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='more' and contains(.,'More')]")))
while True:
    try:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='more' and contains(.,'More')]"))).click()
        print("MORE button clicked")
    except TimeoutException:
        break
driver.quit()

  • 控制台输出:

  • Console Output:

    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    MORE button clicked
    

  • 这篇关于点击“更多"通过硒按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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