如何用Python Selenium渲染Power BI切片器选项
问题:Power BI仪表板切片器滚动无法渲染隐藏月份
我需要爬取某Power BI仪表板第二页的数据,设置日期切片器时,展开年份选项后,部分月份因超出可视范围未被渲染,尝试多种滚动方法均无法触发这些月份的加载。
当前已完成的操作代码
# Selenium 资源导入 from selenium import webdriver from selenium.webdriver.edge.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service # 补充原代码缺失的Service导入 # 驱动文件路径 driver_file = r"d:\dev\selenium\msedgedriver.exe" # 可替换为其他可用浏览器驱动 service = Service(driver_file) # 浏览器配置项 options = Options() options.add_experimental_option("detach", True) options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--inprivate") # 启动浏览器 driver = webdriver.Edge(service = service, options = options) # 打开目标仪表板URL url = "https://app.powerbi.com/view?r=eyJrIjoiZWIzNDg3YzUtMGFlMC00MzdmLTgzOWQtZThkOWExNTU2NjBlIiwidCI6IjQ0OTlmNGZmLTI0YTYtNGI0Mi1iN2VmLTEyNGFmY2FkYzkxMyJ9" driver.get(url) # 跳转到第二页 driver.find_element(By.XPATH, '//button[@aria-label="Próxima Página"]/i').click() # 打开日期切片器面板 driver.find_element(By.XPATH, '//div[@class="slicer-dropdown-menu"]/i').click() # 补充原代码缺失的click()操作 # 展开2024年的月份选项列表 driver.find_element(By.XPATH, '//div[@class="slicerItemContainer" and @title="2024"]/div[@class="expandButton"]').click()
执行上述代码后,切片器展开2024年的月份,但仅能看到3月及之前的选项,后续月份因未被渲染无法选择。
已尝试的无效方案
- JavaScript操作容器滚动:执行后切片器无任何响应
slicer_container = driver.find_element(By.XPATH, '//div[@class="slicerContainer"]') driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", slicer_container)
- 按键模拟滚动:触发
ElementNotInteractableException异常
from selenium.webdriver.common.keys import Keys scroll_container = driver.find_element(By.CLASS_NAME, "scroll-bar") scroll_container.send_keys(Keys.DOWN)
ElementNotInteractableException: Message: element not interactable (Session info: MicrosoftEdge=135.0.3179.85)
- ActionChains模拟交互滚动:同样抛出
ElementNotInteractableException异常
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys actions = ActionChains(driver) actions.move_to_element(scroll_bar).click().send_keys(Keys.PAGE_DOWN).perform()
ElementNotInteractableException: Message: element not interactable (Session info: MicrosoftEdge=135.0.3179.85)
- 修改元素样式实现视觉滚动:仅能看到滚动效果,但未触发隐藏月份的渲染加载
visible_group = driver.find_element(By.CLASS_NAME, 'visibleGroup') driver.execute_script(f'arguments[0].style.transform = "translateY(-60px)";', visible_group)
求能触发切片器渲染隐藏月份的有效滚动解决方案。
内容的提问来源于stack exchange,提问作者Maurício Schwartsman
相关产品推荐
相关产品推荐

