3

我想从“ https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs= ”网站下载 csv 文件 我正在使用 python 和 selenium 脚本,如下所示:但是我收到异常“ElementNotInteractableException”并且无法下载该页面

    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")
4

2 回答 2

2

这是您的问题的答案:

您所指的元素find_element_by_id("submit-download-list")实际上下载了一个PDF文件。因此,为了这个问题/帖子/线程/讨论的未来程序员和读者的利益,您可以考虑将问题标题更改为Download and Save PDF file using selenium and python from popup

这是使用 selenium 和 python 从弹出窗口下载和保存 PDF 文件的代码块:

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()

让我知道这是否回答了您的问题。

于 2017-07-14T08:37:34.183 回答
0

您将收到异常 ElementNotInteractableException ,因为一旦弹出窗口打开,该元素就可以访问。您缺少单击打开弹出窗口的下载链接。请尝试以下方法,

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")
于 2017-07-14T09:04:17.500 回答