[Python] How to render Power BI slicer items with Python Selenium

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to scrape the second page of this Power BI dashboard. In order to get the data from a specific month, I must set the date in a slicer:

    [​IMG]

    However, when I expand a year element to display the month elements, some are out of sight and thus unrendered. Therefore, I must scroll down the slicer. However, I have yet to find a method that works.

    Before I give further details, this is how I get to the desired page and expand the slicer:

    # Selenium resources
    from selenium import webdriver
    from selenium.webdriver.edge.options import Options
    from selenium.webdriver.common.by import By

    # Driver service
    driver_file = r"d:\dev\selenium\msedgedriver.exe" # or whatever driver is available

    service = Service(driver_file)

    # Browser options
    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")

    # Open browser
    driver = webdriver.Edge(service = service, options = options)

    # Get URL
    url = "https://app.powerbi.com/view?r=eyJrIjoiZWIzNDg3YzUtMGFlMC00MzdmLTgzOWQtZThkOWExNTU2NjBlIiwidCI6IjQ0OTlmNGZmLTI0YTYtNGI0Mi1iN2VmLTEyNGFmY2FkYzkxMyJ9"

    driver.get(url)

    # Proceed to next page
    driver.find_element(By.XPATH, '//button[@aria-label="Próxima Página"]/i').click()

    # Open date slicer
    driver.find_element(By.XPATH, '//div[@class="slicer-dropdown-menu"]/i')

    # Expand month options for a year, e.g. 2024
    (driver \
    .find_element(By.XPATH, '//div[@class="slicerItemContainer" and @title="2024"]/div[@class="expandButton"]') \
    .click())


    And so:

    [​IMG]

    But as it stands, I cannot select any month beyond March.

    The slicer doesn't budge when I execute a JavaScript snippet:

    slicer_container = driver.find_element(By.XPATH, '//div[@class="slicerContainer"]')
    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", slicer_container)


    Trying to scroll down with keys throws an 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 yield the same error:

    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)
    ​

    And finally, I've tried to use a style transform – but it only manages to scroll the element visually and doesn't trigger the rendering of the next slicer items.

    visible_group = driver.find_element(By.CLASS_NAME, 'visibleGroup')
    driver.execute_script(f'arguments[0].style.transform = "translateY(-60px)";', visible_group)


    [​IMG]

    All told, I have no idea what to do. Any ideas? Please feel free to ask for further details.

    Continue reading...

Compartilhe esta Página