【问题标题】:Message: no such element: Unable to locate element: {"method":"css selector","selector":".pagination-shortcut-link"} clicking on next page link消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":".pagination-shortcut-link"} 点击下一页链接
【发布时间】:2023-03-12 05:35:01
【问题描述】:

我正在尝试学习如何使用 Python 抓取新闻标题,并阅读我发现的这篇文章:https://medium.com/analytics-vidhya/how-to-scrape-news-headlines-from-reuters-27c0274dc13c

它工作得很好,但是当我尝试用其他新闻页面模拟它时,我继续得到没有这样的元素错误。我意识到这是因为我在 html 中选择了错误的类元素,但是我不明白我应该选择什么其他类。

本新闻页面使用了以上脚本:https://www.reuters.com/news/archive/technologynews?view=page&page=6&pageSize=10

我试图在以下页面上使用它,特别是调查当地的国家机构:

https://www.startribune.com/search/?page=1&q=%22Department%20of%20Human%20Services%22&refresh=true

https://www.twincities.com/?s=%22Department+of+Human+Services%22&orderby=date&order=desc

这是代码,其中唯一的变化是用我正在研究的第一个网页替换路透社网页,并替换按钮选择的类元素:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import dateutil.parser
import time
import csv
from datetime import datetime
import io
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.startribune.com/search/?page=1&q=%22Department%20of%20Human%20Services%22&refresh=true')

count = 0
headlines =[]
dates = []
for x in range(500):    
    try:
        # loadMoreButton.click()
        # time.sleep(3)
        loadMoreButton = driver.find_element_by_class_name("pagination-shortcut-link")
        # driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        time.sleep(3)
        loadMoreButton.click()
        time.sleep(2)
        news_headlines = driver.find_elements_by_class_name("story-title")
        news_dates = driver.find_elements_by_class_name("timestamp")
        for headline in news_headlines:
            headlines.append(headline.text)
            print(headline.text)
        for date in news_dates:
            dates.append(date.text)
            print(date.text)
            count=count+1
            print("CLICKED!!:")
    except Exception as e:
        print(e)
        break

为了获得类名,我右键单击下一步按钮并选择检查元素并复制我看到的内容。但是我继续收到错误。我不确定我打算使用什么其他类元素。

【问题讨论】:

  • 如果一个选择器不适合您,也许可以尝试使用另一个(XPath、标签名称等)
  • 我对这一切都很陌生,所以我不太确定你的意思。我应该阅读 find_element_by_class_name 函数的文档吗?你知道有哪些页面详细介绍了你提到的内容吗?

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

要点击下一页的链接,你需要诱导WebDriverWaitelement_to_be_clickable(),你可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    driver.get('https://www.startribune.com/search/?page=1&q=%22Department%20of%20Human%20Services%22&refresh=true')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.pagination-list-item.is-selected +li > a"))).click()
    
  • 使用XPATH

    driver.get('https://www.startribune.com/search/?page=1&q=%22Department%20of%20Human%20Services%22&refresh=true')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='pagination-list-item is-selected']//following::li[1]/a"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


参考文献

您可以在NoSuchElementException 上找到一些相关讨论:

【讨论】:

  • 感谢您的回复!只是为了澄清,您的建议还需要稍后对 loadMoreButton 变量进行进一步修改?我最初尝试仅添加 WebDriverWait 行;它加载了第二页,但脚本在没有阅读第一页的任何标题的情况下停在那里。我还尝试更改在 loadMoreButton 中选择的类元素,结果相似。
【解决方案2】:

您访问的每个网页的类名称都会发生变化,因为是开发人员自己为特定的 WebElement 选择名称,您至少应该在使用 Selenium 之前先了解基本的 HTML。当您更改要抓取的网页时,您必须经常(如果不是总是)更改您的代码。我建议您在使用 Selenium 时也不要依赖 ID,因为开发人员可以根据需要更改它们,例如,Google 网站有这样做的算法,因此您的代码现在可以工作,但即使在同一个网页上也不能在第二天工作. 最好检查元素内的静态文本或在短时间内可能不会改变的东西。 例如,如果您需要单击上面写有“下一步”的按钮,请抓取网页中的所有按钮并循环遍历它们,检查带有“下一步”文本的按钮并单击()。

在这里查看我的答案:How to click on the Ask to join button within https://meet.google.com using Selenium and Python?

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多