使用Selenium WebDriver导出Power BI仪表板数据时遇错误求助
Power BI仪表板表格数据导出自动化问题(Selenium + Python)
自动化目标
- 导航至指定Power BI仪表板URL
- 定位仪表板中的表格
- 点击表格旁的“更多选项”按钮(显示为“...”)
- 点击弹出的“Export data”选项
- 处理点击“Export data”后出现的对话框/模态框
- 点击最终的“Export”按钮启动下载
- 验证导出文件
exported_data.csv是否存在于下载文件夹中
当前报错信息
ERROR:__main__:Error occurred: Message: Stacktrace: GetHandleVerifier [0x00007FF776C08132+13538] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF776B91DE9+595465] (No symbol) [0x00007FF7769AE6CF] (No symbol) [0x00007FF7769F2960] (No symbol) [0x00007FF7769F2A1F] (No symbol) [0x00007FF776A2D627] (No symbol) [0x00007FF776A1203F] (No symbol) [0x00007FF7769E8147] (No symbol) [0x00007FF776A2B1EE] (No symbol) [0x00007FF776A11C63] (No symbol) [0x00007FF7769E766E] (No symbol) [0x00007FF7769E683C] (No symbol) [0x00007FF7769E7221] Microsoft::Applications::Events::EventProperty::to_string [0x00007FF776DC96D4+1099860] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF776B0D8FC+53532] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF776B00E25+1605] Microsoft::Applications::Events::EventProperty::to_string [0x00007FF776DC8665+1095653] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF776B9C961+27777] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF776B96CE4+4100] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF776B96E1B+4411] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF776B8CFA0+575424] BaseThreadInitThunk [0x00007FF82639257D+29] RtlUserThreadStart [0x00007FF82660AF28+40]
我的代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import os import time import logging # Setup logging logging.basicConfig(level=logging.INFO) # Set logging level to INFO logger = logging.getLogger(__name__) # Initialize the WebDriver (using Microsoft Edge in this example) driver = webdriver.Edge() try: # Open the Power BI dashboard driver.get("https://app.powerbi.com/groups/me/apps/abcdwd-dddddd2-4dc0-9a2e-e3a701c76f0b/reports/.......") # Wait for the dashboard to load WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, "reportContainer"))) # Locate the table within the dashboard using the provided class table_locator = (By.CSS_SELECTOR, 'div.visualWrapper.report') table_element = WebDriverWait(driver, 30).until(EC.presence_of_element_located(table_locator)) # Find the "more options" button next to the table and click it more_options_button_locator = (By.CSS_SELECTOR, 'button[data-testid="visual-more-options-btn"]') more_options_button = table_element.find_element(*more_options_button_locator) more_options_button.click() # Wait for the "Export data" option to appear and click it export_data_locator = (By.CSS_SELECTOR, 'button[data-testid="pbimenu-item.Export data"]') export_data_button = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(export_data_locator)) export_data_button.click() # Now handle the dialog/modal that appears after clicking "Export data" # Assuming there's no iframe switch needed, adjust if necessary # Wait for the final "Export" button and click it final_export_locator = (By.CSS_SELECTOR, 'button[data-testid="export-btn"]') final_export_button = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(final_export_locator)) final_export_button.click() # Wait for some time to allow the export process to complete (adjust as needed) time.sleep(10) # Example: Check if the exported file exists in the downloads folder download_path = os.path.expanduser('~\\Downloads') # Adjust for different OS if necessary exported_file = os.path.join(download_path, 'exported_data.csv') if os.path.exists(exported_file): logger.info("Exported file found at: %s", exported_file) else: logger.error("Exported file not found. Check export process or download manually.") except Exception as e: logger.error(f"Error occurred: {str(e)}") finally: # Close the WebDriver driver.quit()
问题排查与修复建议
1. 元素定位不准确
Power BI的UI元素属性可能随版本更新变化,需重新定位:
- 表格选择器
div.visualWrapper.report可能匹配多个元素,改为结合表格标题的精准选择器,比如:div.visualWrapper.report:has(.visualTitleTextContainer:contains("你的表格标题"))(需启用浏览器:has选择器支持) - 替换
presence_of_element_located为visibility_of_element_located,确保元素可见后再操作
2. 未处理登录状态
Power BI需要登录权限,可通过加载浏览器已保存的会话跳过登录:
from selenium.webdriver.edge.options import Options options = Options() # 替换为你的Edge用户数据路径 options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Microsoft\Edge\User Data") driver = webdriver.Edge(options=options)
3. 模态框iframe切换
点击Export data后的弹窗可能在iframe中,需先切换上下文:
# 点击Export data后等待iframe加载并切换 WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Export Data']"))) # 定位并点击导出按钮 final_export_button = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(final_export_locator)) final_export_button.click() # 切回主文档 driver.switch_to.default_content()
4. 优化文件等待逻辑
替换固定time.sleep为动态等待,处理可能的文件重命名:
import glob import time download_path = os.path.expanduser('~\\Downloads') exported_file_pattern = os.path.join(download_path, 'exported_data*.csv') timeout = 60 start_time = time.time() while time.time() - start_time < timeout: files = glob.glob(exported_file_pattern) if files: logger.info(f"Exported file found at: {files[0]}") break time.sleep(2) else: logger.error("Exported file not found within timeout period.")
5. 匹配WebDriver版本
确保Edge WebDriver版本与本地Edge浏览器版本完全一致,避免兼容性问题。
内容的提问来源于stack exchange,提问作者Nhu Tuyen Nguyen
相关产品推荐
相关产品推荐

