Selenium Python Chrome下载PDF被拦截问题求助
Chrome拦截Selenium下载PDF的解决方法
我尝试从http://www.cub.org.br/cub-m2-estadual/CE/下载PDF文件,点击“Gerar Relatório em PDF”(生成PDF报告)时,Chrome提示文件不可信而拦截下载。已尝试多种Selenium Python配置绕过安全提示,但问题仍未解决,代码如下:
import os import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import Select from selenium.webdriver.common.by import By # Pasta Padrão download_dir = os.getcwd() # Instância Chrome Options chrome_options = Options() # Configurando Chrome Para Permitir Dowload do PDF chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-plugins") chrome_options.add_argument("--incognito") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--disable-popup-blocking") chrome_options.add_argument("--ignore-certificate-errors") chrome_options.add_argument("--allow-insecure-localhost") # Pasta Download prefs = { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True, "download.open_pdf_in_system_reader": False, "profile.default_content_settings.popups": 0, "safebrowsing.enabled": True } chrome_options.add_experimental_option('prefs', prefs) # Instância Chrome driver = webdriver.Chrome(options=chrome_options) driver.maximize_window() time.sleep(2) # Acessando Site driver.get('http://www.cub.org.br/cub-m2-estadual/CE/') time.sleep(2) # Verifica Anos Disponíveis # Na Lista Suspensa lista_suspensa = Select(driver.find_element(By.NAME, 'ano')) opcoes_lista_suspensa = [item.text for item in lista_suspensa.options] # Seleciona Primeiro Elemento da Lista # Que é o Ano Mais Recente lista_suspensa.select_by_value(opcoes_lista_suspensa[0]) # Clicando Baixar Relatório download_file_xpath = '//*[@id="wrapper"]/div/div[1]/div/div[1]/div[2]/div/form/input[2]' driver.find_element(By.XPATH, download_file_xpath).click()
解决方案
关闭安全浏览保护:当前配置中
safebrowsing.enabled设为True会触发Chrome的安全拦截,修改为False并添加禁用下载保护的配置:prefs = { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True, "download.open_pdf_in_system_reader": False, "profile.default_content_settings.popups": 0, "safebrowsing.enabled": False, "safebrowsing.disable_download_protection": True }添加安全相关启动参数:在ChromeOptions中加入以下参数,进一步禁用安全检查机制:
chrome_options.add_argument("--safebrowsing-disable-download-protection") chrome_options.add_argument("--safebrowsing-disable-extension-blacklist")替换隐式等待为显式等待:
time.sleep()依赖固定时长,改用显式等待确保元素加载完成后再操作,避免页面未就绪导致的异常:from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 等待下拉框加载完成 lista_suspensa = Select(WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, 'ano')) )) # 等待下载按钮可点击后执行点击 download_btn = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="wrapper"]/div/div[1]/div/div[1]/div[2]/div/form/input[2]')) ) download_btn.click()检查下载目录权限:确保脚本对指定的下载目录有写入权限,权限不足可能导致下载失败并被Chrome判定为安全风险。
内容的提问来源于stack exchange,提问作者Felipe Gomes
相关产品推荐
相关产品推荐

