无法使用 xpath 和 id 在网页中定位元素

Unable to locate element in webpage using xpath and id

from classes.main import *

url = "https://tofino.civicweb.net/filepro/documents/855?expanded=100967"

with Display(visible=False, size=(1200,1500)):
  print("display initiated")
  browser.get(url)
  print("browser loaded")
  sleep(5)

  browser.find_element_by_xpath("/html/body/div[1]/div/div/div/main/div/div[2]/div/div/div[2]/div[1]/div[2]/div[4]/ul/li[1]/div/span[3]/div/span[1]/div[1]/a").click()
  print("2020 folder clicked")
  sleep(5)

  browser.find_element_by_class_name("document-link-container").click()
  print("Top pdf document clicked")
  sleep(10)

  browser.find_element_by_id("ClicktoDownLoadnotice").click()
  sleep(5)

  browser.find_element_by_id("maskedImage").click()

renamefile('tofino','pdf')

我正在尝试为包含的 url 中的 2020 年文件夹中的第一个 pdf 编写一个网络抓取工具(这样每个月它都会下载最上面的文件,它会发生变化)。无论我使用什么来查找 Web 元素(xpath、id、class...)

,我都会收到以下错误
Traceback (most recent call last):
  File "test_tofino.py", line 19, in <module>
    browser.find_element_by_id("ClicktoDownLoadnotice").click()
  File "/home/angela/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/home/angela/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/home/angela/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/angela/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="ClicktoDownLoadnotice"]

关于如何解决这个问题有什么想法吗?

要单击文本为 Click to Open Full PDF 的元素以获得 2020 年文件夹中的第一个 pdf,您可以使用以下 :

  • 使用CSS_SELECTOR:

    driver.get("https://tofino.civicweb.net/filepro/documents/855?expanded=100967")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.k-group > li.k-item a.document-link"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[title='Click to Open Full PDF']"))).click()
    
  • 使用XPATH:

    driver.get("https://tofino.civicweb.net/filepro/documents/855?expanded=100967")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='k-group']/li[@class='k-item']//a[@class='document-link']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@title='Click to Open Full PDF']"))).click()
    
  • 注意:您必须添加以下导入:

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