如何使用 Selenium 和 Python 单击“查看全部”按钮

How to click on the See all button using Selenium and Python

这个在 Python 3 中使用 Selenium 的脚本帮助我通过单击“查看全部”按钮从页面中抓取数据,但它停止工作,我不知道原因。你能帮我看看代码吗?

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

nav= Service(ChromeDriverManager().install())
driver= webdriver.Chrome(service= nav)

driver.get('https://getdaytrends.com/es/venezuela/#moreTrends')
                   
driver.find_element(By.CLASS_NAME,'btn btn-outline-primary px-5')
driver.execute_script("arguments[0].click();", search)

datos= driver.find('table')

追溯是:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-outline-primary px-5"}

要单击 Ver todo 34,您需要 scrollIntoView() 所需的元素并引入 WebDriverWait for the and you can use either of the following :

driver.get("https://getdaytrends.com/es/venezuela/#moreTrends")
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='icon icon-longest-lasting']")))
driver.execute_script("arguments[0].scrollIntoView(true);", element)
WebDriverWait(driver, 7).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-outline-primary px-5']"))).click()

浏览器快照:

注意:您必须添加以下导入:

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

参考资料

您可以在以下位置找到关于 的一些相关讨论: