Download and save multiple csv files using selenium and python from popup

回眸只為那壹抹淺笑 提交于 2019-11-26 11:36:36

问题


I want to download csv files from \"https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=\" website I am using python and selenium script as written below:But i get the exception \"ElementNotInteractableException\" and unable to download the page

    from selenium import webdriver
    fp=webdriver.FirefoxProfile()
    fp.set_preference(\"browser.helperApps.neverAsk.saveToDisk\",\"text/csv\")
    browser = webdriver.Firefox(fp)
    browser.get(\"https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=\")
    browser.find_element_by_id(\"submit-download-list\")

回答1:


Here is the Answer to your Question:

The element you referred as find_element_by_id("submit-download-list") actually downloads a PDF file. So for the benefit of future programmers and readers of this question/post/thread/discussion, you may consider to change your question header to Download and Save PDF file using selenium and python from popup

Here is the code block to Download and Save PDF file using selenium and python from popup:

import os
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
newpath = 'C:\\home\\DebanjanB'
if not os.path.exists(newpath):
    os.makedirs(newpath)

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir",newpath)
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", "")
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.manager.closeWhenDone", True)
profile.set_preference("pdfjs.disabled", True)

caps = DesiredCapabilities.FIREFOX
browser = webdriver.Firefox(firefox_profile=profile, capabilities=caps, firefox_binary=binary, executable_path='C:\\Utility\\BrowserDrivers\\geckodriver.exe')
browser.maximize_window()
browser.get("https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=")
browser.find_element_by_id("save-list-link").click()
download_link = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.XPATH, "//input[@id='submit-download-list']"))
)
download_link.click()

Let me know if this Answers your Question.




回答2:


You are getting the exception ElementNotInteractableException because the element will be accessible after once the popup opens up. You are missing to click the download link which opens up the popup. please try the following,

from selenium import webdriver
fp=webdriver.FirefoxProfile()
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
browser = webdriver.Firefox(fp)
browser.get("https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=")
browser.find_element_by_id("save-list-link").click()
browser.find_element_by_id("submit-download-list")


来源:https://stackoverflow.com/questions/45097302/download-and-save-multiple-csv-files-using-selenium-and-python-from-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!