pythonseleniumpdfwebdriverchrome-web-driver

How to iterate and download multiple pdfs using selenium and Python


I am a bit new to using selenium and Python.Below is the code that I am trying to run to download multiple files.

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe')
cusip=['abc123','def456','ghi789']
for a in cusip:

    page=driver.get("http://mylink=" + str(a) + ".pdf")
    with open(a + '.pdf', 'wb') as f:
        for chunk in page.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)

Error that I receive is as below:

Traceback (most recent call last):
  File "C:/Users/shashi.singh/PycharmProjects/HiSSS/Selenium.py", line 13, in <module>
    for chunk in page.iter_content(chunk_size=1024):
AttributeError: 'NoneType' object has no attribute 'iter_content'

Solution

  • Thanks for the help everyone..Below is the code that I am using and its working fine.

    from selenium import webdriver
    driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe')
    cusip=['abc123','def456','ghi789']
    options = webdriver.ChromeOptions()
    
    tgt = "C:\\directory"  #target directory to download item
    profile = {"plugins.plugins_list": [{"enabled":False, "name":"Chrome PDF Viewer"}],
        "download.default_directory" : tgt}
    options.add_experimental_option("prefs",profile)
    print(options)
    driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe', chrome_options=options)
    
    for a in cusip:
        page=driver.get("http://mylink=" + str(a) + ".pdf") #iterate the item in cusip list
    
    Print('Process completed Successfully')
    

    The cusip is a list that I have to iterate and add it to the web page I need to download and hence you may modify it needed.