如何用Selenium批量导出SSRS报表为PDF?遇节点丢失错误求助
问题:SSRS批量导出PDF时循环执行中元素ID丢失导致定位失败
我需要从供应商搭建的SSRS(SQL Server Reporting Services)网站批量保存PDF报表,操作流程为:
- 设置起止日期
- 通过下拉菜单选择客户
- 点击查看报表按钮生成报表
- 将报表另存为PDF
最后一步点击PDF按钮时,会通过Javascript:void(0)打开新标签页,该标签页随后自动关闭并触发文件下载。目前单份PDF生成和下拉菜单切换操作均能成功,但循环执行PDF导出请求时,用于定位下拉菜单的ID丢失,触发如下错误:
selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"message":"No node with given id found"}
我使用的代码如下:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.select import Select import time from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By options = Options() prefs = { "download.prompt_for_download": False, "download.directory_upgrade": True, "plugins.always_open_pdf_externally": True } options.add_experimental_option("prefs", prefs) options.add_experimental_option("detach", True) driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) driver.maximize_window() driver.get("https:\\SSRSwebsite\report\report") #Site has been removed for security reasons. #A short moment to fill in date manually. Will be improved later on. time.sleep(10) #Find frame where ID could be found. iframe = driver.find_element(By.XPATH, '//iframe[@class="viewer"]') driver.switch_to.frame(iframe) #location of pulldown menu get all selections that are possible. listofelements = driver.find_elements(By.XPATH,'//*[@name="ReportViewerControl$ctl04$ctl07$ddValue"]/option') #Loop to fill in the each option and create the PDF file. for i in range(1, len(listofelements)): time.sleep(10) #Fill in dropdown menu Dropdown = Select(driver.find_element(By.ID,"ReportViewerControl_ctl04_ctl07_ddValue")) Dropdown.select_by_value(listofelements[i].get_attribute("value")) #button to create report driver.find_element(By.XPATH, "//input[@id='ReportViewerControl_ctl04_ctl00']").click() time.sleep(3) #Open save menu driver.find_element(By.ID, "ReportViewerControl_ctl05_ctl04_ctl00").click() #Create PDF to download. This button creates a new tab and closed directly after is has been created. driver.find_element(By.XPATH, "//a[@title='PDF']").click()
已尝试的解决方法:
- 点击PDF按钮后立即切换回第一个标签页
- 在不同标签页分别打开页面并逐个执行操作
内容的提问来源于stack exchange,提问作者Koen Alferink
相关产品推荐
相关产品推荐

