I'm trying to download pdf from pubmed central using selenium webdriver in python. The code is provided below which downloads the pdf fine but, it does not quit automatically. i.e. it is sort of stuck executing browser.get(href)
line in download_pmc_pdf()
. What could be the reason?
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
ncbi_url = "https://www.ncbi.nlm.nih.gov/"
download_dir = "pdf_downloaded"
if not os.path.exists(download_dir):
os.mkdir(download_dir)
options = Options()
options.set_preference('permissions.default.stylesheet', 2)
options.set_preference('permissions.default.image', 2)
options.set_preference('browser.download.folderList', 2)
options.set_preference('browser.download.dir', os.path.abspath(download_dir))
options.set_preference('browser.download.manager.showWhenStarting', False)
options.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
options.set_preference('pdfjs.disabled', True)
browser = webdriver.Firefox(options=options)
###############################################################################
def download_pmc_pdf(pmcid_key):
pmc_url = ncbi_url + "pmc/articles/" + pmcid_key + "/"
browser.get(pmc_url)
try:
wait = WebDriverWait(browser, 10)
pdf_links = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.pmc-sidebar__formats li a')))
# Iterate over all links and navigate to the link containing '.pdf' in the href attribute
for link in pdf_links:
href = link.get_attribute('href')
if '.pdf' in href:
browser.get(href)
break # exit the loop as we have found the PDF link
print("line beofre browser.quit()")
browser.quit() # quit the browser outside the loop
except Exception as e:
print(e)
###############################################################################
pmcid = "2788555"
download_pmc_pdf(pmcid)
browser.quit()
get
for anything other than HTML.) I don't suppose the e-utils support PDF paper download, do they? $\endgroup$