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

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...

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...