0

New to Python and having trouble figuring this out, any help is much appreciated.

I am using Selenium to grab ~150 PDFs and saving to a directory automatically, but the names are not descriptive.

The process is:

  • iterate through a drop-down menu
  • generate a PDF report that opens in a new tab
  • save the report to a directory

Is there a way to name the files before saving by using the values in the drop-down?

Here is my setup:

# Set options:

chrome_options = Options()

download_dir = "C:\\Me\MyDirectory\Folder"

chrome_options.add_experimental_option('prefs',  {
    "download.default_directory": download_dir,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "plugins.always_open_pdf_externally": True
    }
)

b = webdriver.Chrome("C:\\Users\me\Downloads\chromedriver_win32\chromedriver.exe", options = chrome_options)

# Begin download

b.get('https://something.com')

inputs = Select(b.find_element_by_css_selector('#SpecialtyCode'))

input1 = len(inputs.options)

for items in range(input1):
    inputs.select_by_index(items)
    button = b.find_element_by_id('btnView')
    button.click()
Matt
  • 7,255
  • 2
  • 12
  • 34
  • No. You cannot specify name of downloading file while auto download using selenium. What you could after downloading the each file you could check the latest file in the downloaded directory and then changed the filename using your select option. – KunduK Mar 01 '21 at 14:16
  • Ah ok, thanks for the comment. Any advice on an approach to renaming in the directory? – Matt Mar 01 '21 at 14:25
  • Accepted answer should resolved your query https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading – KunduK Mar 01 '21 at 14:43

1 Answers1

0

You need to call get_attribute method, and with it you can get the attribute value.

elem= driver.find_element_by_id('org')
attribute_val = elem.get_attribute("attribute name")

Also if you need a text from dropdown items to be part of your file name, you can just use:

text = driver.find_element_by_class_name("current-stage").getText("my text")
Gaj Julije
  • 1,538
  • 15
  • 32