Skip to main content

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

Popular posts from this blog

Prop `className` did not match in next js app

I have written a sample code ( Github Link here ). this is a simple next js app, but giving me error when I refresh the page. This seems to be the common problem and I tried the fix provided in the internet but does not seem to fix my issue. The error is Warning: Prop className did not match. Server: "MuiBox-root MuiBox-root-1" Client: "MuiBox-root MuiBox-root-2". Did changes for _document.js, modified _app.js as mentioned in official website and solutions in stackoverflow. but nothing seems to work. Could someone take a look and help me whats wrong with the code? Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console