如何使用python截取特定网址的屏幕截图

问题描述 投票:0回答:1

我需要使用 python 截取 Power BI 报告的屏幕截图,我想传递 Power BI URL。该 URL 将具有如下流程 -> 电子邮件 -> 提交,然后 密码 -> 登录,然后 保持登录 -> 是 我已经尝试过下面的代码。我必须在 databricks 中运行这个脚本,这将是自动化的,它将在每天的特定时间运行

import datetime
import time
import os
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


def login_powerbi(username, password):
    # Set up the ChromeDriver path in the PATH environment variable
    chromedriver_path = 'C:/Users/Desktop/chrome_driver/chromedriver.exe'
    os.environ["PATH"] += os.pathsep + chromedriver_path

    # Set up the webdriver
    driver = webdriver.Chrome()

    try:
        # Open the Power BI report URL (login page)
        driver.get("https://your_powerbi_url_here")

        # Wait for the login page to load
        wait = WebDriverWait(driver, 10)
        email_field = wait.until(EC.presence_of_element_located((By.ID, "email")))
        email_field.send_keys(username)

        # Submit the email form
        email_form = driver.find_element_by_id("i0116")
        email_form.submit()

        # Wait for the password field to load and then enter the password
        password_field = wait.until(EC.presence_of_element_located((By.ID, "i0118"))) #i0118
        password_field.send_keys(password)

        # Submit the password form
        password_form = driver.find_element_by_id("i0118") #idSIButton9
        password_form.submit()

        # Wait for the "Stay signed in?" prompt to load and then click "Yes"
        stay_signed_in_prompt = wait.until(EC.presence_of_element_located((By.ID, "idSIButton9"))) #idSIButton9
        stay_signed_in_prompt.click()

        # Wait for the Power BI report to load after authentication
        driver.implicitly_wait(30)

        # Add a delay before taking the screenshot to ensure the report is fully loaded
        time.sleep(10)

        # Take the screenshot
        now = datetime.datetime.now()
        file_name = f'screenshot_{now.strftime("%Y-%m-%d_%H-%M-%S")}.png'
        output_path = "C:/Users/Desktop/Screenshot/output/"

        screenshot_output_file = f'{output_path}/{file_name}'
        driver.save_screenshot(screenshot_output_file)

        print(f"Screenshot saved as {screenshot_output_file}")

    except Exception as e:
        print(f"Error during login and screenshot capture: {e}")

    finally:
        # Close the browser
        driver.quit()

if __name__ == "__main__":
    # Replace 'your_username' and 'your_password' with your actual Power BI login credentials
    login_powerbi("your_username", "your_password")


但是上面的代码给出了以下错误,可能的解决方案是什么

Error during login and screenshot capture: Message: element not interactable
  (Session info: chrome=115.0.5790.110)
Stacktrace:
Backtrace:
        GetHandleVerifier [0x0063A813+48355]
        (No symbol) [0x005CC4B1]
        (No symbol) [0x004D5220]
        (No symbol) [0x004FD046]
        (No symbol) [0x004FC8B1]
        (No symbol) [0x0051A73C]
        (No symbol) [0x004F9A36]
        (No symbol) [0x0051AA94]
        (No symbol) [0x0052C922]
        (No symbol) [0x0051A536]
        (No symbol) [0x004F82DC]
        (No symbol) [0x004F93DD]
        GetHandleVerifier [0x0089AABD+2539405]
        GetHandleVerifier [0x008DA78F+2800735]
        GetHandleVerifier [0x008D456C+2775612]
        GetHandleVerifier [0x006C51E0+616112]
        (No symbol) [0x005D5F8C]
        (No symbol) [0x005D2328]
        (No symbol) [0x005D240B]
        (No symbol) [0x005C4FF7]
        (No symbol) [0x76FE00C9]
        RtlGetAppContainerNamedObjectPath [0x77587B1E+286]
        RtlGetAppContainerNamedObjectPath [0x77587AEE+238]

[ERROR:ssl_client_socket_impl.cc(980)] handshake failed; returned -1, SSL error code 1, net_error -3

python python-3.x selenium-webdriver url
1个回答
0
投票

尝试使用密码字段的方法:

actionChains = ActionChains(driver)

password_field = wait.until(EC.presence_of_element_located((By.ID, "i0118"))) 
actionChains.click(password_field).send_keys('your-password').perform()

通常,如果元素不可直接交互,可以使用

ActionChains

与其交互
© www.soinside.com 2019 - 2024. All rights reserved.