You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python/Selenium抓取Power BI仪表盘数据遇阻求助

Power BI仪表盘Selenium抓取问题解决指南

我尝试用Python的Selenium库抓取某Power BI仪表盘数据,操作流程是:先选择一个Job Name(职位名称),再勾选State(州)的“全选”选项,最后抓取页面底部加载的表格数据。

Power BI仪表盘截图

目前我的代码无法定位选择控件,也没法抓取可滚动表格里的数据,求技术帮助或提示。

我的代码如下:

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

driver = webdriver.Chrome('chromedriver')
driver.get("https://app.powerbi.com/view?r=eyJrIjoiNzA0MGM4NGMtN2E5Ny00NDU3LWJiNzMtOWFlMGIyMDczZjg2IiwidCI6IjM4MmZiOGIwLTRkYzMtNDEwNy04MGJkLTM1OTViMjQzMmZhZSIsImMiOjZ9&pageName=ReportSection")

# Find and click the first two elements
##occupation
wait_for_element(By.XPATH, "//body/div[@id='pbiAppPlaceHolder']/report-embed[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/exploration-container[1]/div[1]/div[1]/docking-container[1]/div[1]/div[1]/div[1]/div[1]/exploration-host[1]/div[1]/div[1]/exploration[1]/div[1]/explore-canvas[1]/div[1]/div[2]/div[1]/div[1]").click()
##all states
wait_for_element(By.XPATH, "//body/div[@id='pbiAppPlaceHolder']/report-embed[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/exploration-container[1]/div[1]/div[1]/docking-container[1]/div[1]/div[1]/div[1]/div[1]/exploration-host[1]/div[1]/div[1]/exploration[1]/div[1]/explore-canvas[1]/div[1]/div[2]/div[1]/div[1]").click()

问题分析与修复方案

1. 控件定位失败的核心问题与修复

  • 当前代码的致命错误:两次点击用了完全相同的XPATH,Job Name和State是两个不同控件,路径不可能一致;且你使用的长XPATH依赖DOM层级结构,Power BI组件是动态渲染的,DOM结构随时可能变化,这种定位方式极不稳定。
  • 优化定位策略:
    • 优先使用Power BI组件自带的属性(如aria-label、data-testid)或文本内容定位,避免依赖DOM层级。
    • 补充wait_for_element的正确实现:你代码里调用的wait_for_element未定义,必须用WebDriverWait结合预期条件来等待元素。
  • 修正后的定位代码示例:
    # 等待并点击Job Name下拉控件
    job_name_dropdown = WebDriverWait(driver, 15).until(
        EC.element_to_be_clickable((By.XPATH, "//*[@aria-label='Job Name']"))
    )
    job_name_dropdown.click()
    
    # 选择第一个Job Name选项
    first_job_option = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'pbi-dropdown-option')][1]"))
    )
    first_job_option.click()
    
    # 等待并点击State的"全选"按钮
    state_select_all = WebDriverWait(driver, 15).until(
        EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), '全选')]"))
    )
    state_select_all.click()
    

2. 可滚动表格数据抓取方案

Power BI表格采用虚拟滚动,仅渲染可见区域的行,直接抓取只能拿到当前显示的内容,需模拟滚动加载所有行:

import time

# 等待表格加载完成
table = WebDriverWait(driver, 15).until(
    EC.presence_of_element_located((By.XPATH, "//table[contains(@class, 'pivotTable')]"))
)

# 滚动表格加载所有数据
last_scroll_height = driver.execute_script("return arguments[0].scrollHeight", table)
while True:
    # 滚动到表格底部
    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", table)
    # 等待新行加载(根据页面速度调整时间)
    time.sleep(2)
    current_scroll_height = driver.execute_script("return arguments[0].scrollHeight", table)
    # 高度不变说明已加载完所有数据
    if current_scroll_height == last_scroll_height:
        break
    last_scroll_height = current_scroll_height

# 抓取所有行数据
rows = table.find_elements(By.TAG_NAME, "tr")
for row in rows:
    columns = row.find_elements(By.TAG_NAME, "td")
    row_data = [col.text for col in columns]
    print(row_data)

3. 额外注意事项

  • Power BI页面加载较慢,必须全程用WebDriverWait等待元素,禁止使用固定time.sleep(仅滚动后可短暂等待加载)。
  • 若ChromeDriver路径报错,建议改用webdriver.Chrome()并确保ChromeDriver已添加到系统环境变量。
  • 如果遇到iframe嵌套,需先切换到对应iframe再操作元素:driver.switch_to.frame(driver.find_element(By.TAG_NAME, "iframe"))。

内容的提问来源于stack exchange,提问作者Code

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 12:07:19