0

I am not able to find or access any element on the following webpage

https://www.eaton.com/content/dam/eaton/products/conduit-cable-and-wire-management/crouse-hinds/catalog-pages/crouse-hinds-locknuts-nipples-washers-reducers-plugs-rigid-catalog-page.pdf

I am trying to download the file by accessing the download icon but I am not able to do that. I am getting 'No element found' error or 'Timeout exception' if I am waiting until the element is found.

There is no iframe present. Can anyone help me here to download the file using python selenium ?

I have written following script ... need help

from selenium import webdriver
import os.path  # to get Employee ID
import time
from selenium.webdriver.chrome.options import Options  # to change the download location
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Get the Employee ID
EmplyID = os.getenv('USER', os.getenv('USERNAME', 'user'))

# Get the final download location
DownloadPath = (f'C:\\Users\\{EmplyID}\\Desktop\\Catalog files\\Downloaded Files')


chromeOptions = Options()
chromeOptions.add_experimental_option("prefs", {"download.default_directory": DownloadPath})

# create a driver object
driver = webdriver.Chrome(executable_path=f'C:\\Users\\{EmplyID}\\Desktop\\Catalog files\\chromedriver.exe',options=chromeOptions)

# open the pdf
driver.get("https://www.eaton.com/content/dam/eaton/products/conduit-cable-and-wire-management/crouse-hinds/catalog-pages/crouse-hinds-locknuts-nipples-washers-reducers-plugs-rigid-catalog-page.pdf")
driver.maximize_window()
time.sleep(10)

# creating for explicit wait
wait = WebDriverWait(driver,15)

element = wait.until(EC.presence_of_element_located((By.ID, 'download')))
element.click()
time.sleep(5)
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Vishal
  • 1
  • 1
  • Does this answer your question? [Selenium can't find element by XPATH or by CLASS\_NAME even when it exists](https://stackoverflow.com/questions/75894669/selenium-cant-find-element-by-xpath-or-by-class-name-even-when-it-exists) – JeffC Apr 24 '23 at 13:53
  • The download button is under 4 shadow-roots, similar to IFRAMEs but different. The link above has the answer. – JeffC Apr 24 '23 at 13:54

1 Answers1

0

If your purpose is to just download the pdf file from the given URL, you can simply use requests.

import os
import requests

headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"}

# Get the Employee ID
EmplyID = os.getenv('USER', os.getenv('USERNAME', 'user'))

# Get the final download location
DownloadPath = (f'C:\\Users\\{EmplyID}\\Desktop\\Catalog files\\Downloaded Files')

response = requests.get("https://www.eaton.com/content/dam/eaton/products/conduit-cable-and-wire-management/crouse-hinds/catalog-pages/crouse-hinds-locknuts-nipples-washers-reducers-plugs-rigid-catalog-page.pdf",
                    headers=headers)

with open(f"{DownloadPath}\\data.pdf", 'wb') as f:
    f.write(response.content)
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
  • Getting this error Traceback (most recent call last): File "C:\Users\E0571774\Desktop\Python Learning Trial\Programs\Trail 1.py", line 15, in with open(f"{DownloadPath}\\data.pdf", 'wb') as f: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\E0571774\\Desktop\\Catalog files\\Downloaded Files\\data.pdf' – Vishal Apr 24 '23 at 08:08
  • you're getting this error simply because it's not the correct path, it doesn't exist on your machine. Check your DownloadPath – Ajeet Verma Apr 24 '23 at 08:11