selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":XPATH}
A while back, I created this script that downloads a file from a website into a specified folder using Selenium. It's been working fine for the past couple months, but after updating the ChromeDriver to 111.0.5563.64 (March 20, 2023), the code is no longer working, producing an error on line 58:
download_icon = driver.find_element(By.XPATH, dwnicon)
The error says it can't locate the element (which I've asked for it to trace using XPATH) --
Traceback (most recent call last):
File "c:\Users\ujcho\Desktop\Internal Weekly Bluesheet\HomicideReportCollector.py", line 58, in <module>
download_icon = driver.find_element(By.XPATH, dwnicon)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 830, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='icon']/iron-icon"}
(Session info: headless chrome=111.0.5563.65)
Stacktrace:
Backtrace:
(No symbol) [0x0027DCE3]
(No symbol) [0x002139D1]
(No symbol) [0x00124DA8]
(No symbol) [0x0015019F]
(No symbol) [0x001503AB]
(No symbol) [0x0017EE62]
(No symbol) [0x0016AF14]
(No symbol) [0x0017D57C]
(No symbol) [0x0016ACC6]
(No symbol) [0x00146F68]
(No symbol) [0x001480CD]
GetHandleVerifier [0x004F3832+2506274]
GetHandleVerifier [0x00529794+2727300]
GetHandleVerifier [0x0052E36C+2746716]
GetHandleVerifier [0x00326690+617600]
(No symbol) [0x0021C712]
(No symbol) [0x00221FF8]
(No symbol) [0x002220DB]
(No symbol) [0x0022C63B]
BaseThreadInitThunk [0x75D4FA29+25]
DummyExport [0x6CC7CA11+42177]
RtlGetAppContainerNamedObjectPath [0x77DE7A7E+286]
RtlGetAppContainerNamedObjectPath [0x77DE7A4E+238]
(No symbol) [0x00000000]
I have perused the Stack Overflow pages, and I couldn't find a solution to this issue. I've tried re-copying and re-pasting the XPATH for the Download icon to my script, but it's the same. I've also added a driver.implicitly_wait() command before it tries to find the element, but to no avail. Can anyone provide any insight on what may be the source of the problem? I wonder if it has to do with the recent ChromeDriver update. I've pasted the entire code of my script below. Thanks in advance.
from requests import get
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
import shutil
import time
from datetime import date
import pandas as pd
import os
#This script goes into the KCPD Crime Statistics Website and collects the Daily Homicide Analysis Report
#Helper Function to Cause Selenium to Wait to Find the Element Before Downloading
def waitFunction(driver, wait, text):
try:
wait.until(EC.presence_of_element_located((By.XPATH, text)))
except TimeoutException:
return False
#Loading Webdriver
options = webdriver.ChromeOptions()
prefs = {"download.default_directory" : r'C:\Users\ujcho\Desktop\Internal Weekly Bluesheet\Daily KCPD Homicide Reports'}
options.add_experimental_option("prefs", prefs)
options.add_argument('--headless')
chromedriver = Service(r'C:\Users\ujcho\Desktop\Internal Weekly Bluesheet\chromedriver.exe')
driver = webdriver.Chrome(service=chromedriver, options=options)
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
url = "https://www.kcpd.org/crime/crime-statistics/"
wait = WebDriverWait(driver,5)
#Loading necessary variables
today_date = date.today().strftime('%m%d%Y')
print("Today is", today_date)
# driver.get(url)
driver.get(url)
link = "/html/body/main/div/div[2]/section/div/article/div/div/div/div/div/div/table[1]/tbody/tr[1]/td/p/a"
waitFunction(driver,wait,link)
doclink = driver.find_element(By.XPATH, link)
doclink.click()
dwnicon = "//*[@id='icon']/iron-icon"
driver.implicitly_wait(20)
waitFunction(driver,wait,dwnicon)
download_icon = driver.find_element(By.XPATH, dwnicon)
download_icon.click()
time.sleep(10)
alert = Alert(driver)
alert.accept()
source https://stackoverflow.com/questions/75803979/selenium-common-exceptions-nosuchelementexception-message-no-such-element-una
Comments
Post a Comment