0
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')

I am trying to write a webscraper for the first pdf in the year 2020 folder in the included url (so that each month it'll download whatever is the top file, which changes). I get the following error regardless of what I use to find the web element (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"]

Any ideas on how to fix this?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
decoy24601
  • 39
  • 1
  • 4

1 Answers1

1

To click on the element with text as Click to Open Full PDF for the first pdf in the year 2020 folder you can use the following Locator Strategies:

  • Using 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()
    
  • Using 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()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

tofino

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352