思路:用Python网页自动化操作metabase现在发送电子邮件,任务计划程序在指定的时间触发。
操作步骤:
一、metabase做好报表,设置好邮件订阅。
二、编写Python脚本
1、安装selenium、下载浏览器驱动edgedriver
(本文是以Edge浏览器举例操作,注意查看浏览器版本与驱动版本是否匹配)
pip install selenium
2、编写配置文件脚本config.py
# config.py
BROWSER_DRIVER_PATH = r'D:\edgedriver_win64\msedgedriver.exe'
BASE_URL = 'http://127.0.0.1:3000'
USERNAME = '912@qq.com'
PASSWORD = '123456@'
WAIT_TIME = 5
3、编写自动化脚本metabase_automation.py
# metabase_automation.py
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, ElementNotInteractableException
import time
import config
class MetabaseAutomation:
def __init__(self, driver_path, base_url):
self.driver_path = driver_path
self.base_url = base_url
self.driver = self.set_browser()
def set_browser(self):
# 创建EdgeOptions对象
edge_options = Options()
# 禁用沙盒模式
edge_options.add_argument('--no-sandbox')
# 保持浏览器打开状态
edge_options.add_experimental_option('detach', True)
# 设置驱动程序路径
service = Service(self.driver_path)
# 初始化WebDriver
driver = webdriver.Edge(service=service, options=edge_options)
return driver
def login(self, username, password):
# 打开登录页面
self.driver.get(f'{self.base_url}/login')
# 浏览器窗口最大化
self.driver.maximize_window()
# 等待页面加载
time.sleep(config.WAIT_TIME)
# 输入用户名
username_input = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div/main/div/div[2]/div/div[2]/div/form/div[1]/div[2]/input')
username_input.send_keys(username)
# 输入密码
password_input = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div/main/div/div[2]/div/div[2]/div/form/div[2]/div[2]/input')
password_input.send_keys(password)
# 点击登录按钮
login_button = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div/main/div/div[2]/div/div[2]/div/form/button/div/div')
login_button.click()
# 等待登录成功
time.sleep(config.WAIT_TIME)
def send_email(self):
# 打开目标网页
self.driver.get(f'{self.base_url}/dashboard/41')
# 等待页面加载
time.sleep(config.WAIT_TIME)
# 点击邮件订阅按钮
email_subscription_button = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div/main/div/div/div/header/div/div/div[1]/div[2]/div/button[2]')
email_subscription_button.click()
# 等待页面加载
time.sleep(config.WAIT_TIME)
# 点击邮件订阅按钮2
email_subscription_button2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div/main/div/div/div/div/aside/div/div[2]/div/div')
email_subscription_button2.click()
# 等待页面加载
time.sleep(config.WAIT_TIME)
# 点击现在发送邮件按钮
send_email_button = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div/main/div/div/div/div/aside/div[1]/div[3]/div[3]/button')
send_email_button.click()
# 等待操作完成
time.sleep(config.WAIT_TIME)
def close_browser(self):
self.driver.quit()
if __name__ == '__main__':
# 创建自动化实例
automation = MetabaseAutomation(config.BROWSER_DRIVER_PATH, config.BASE_URL)
try:
# 登录
automation.login(config.USERNAME, config.PASSWORD)
# 发送邮件
automation.send_email()
# 等待查看结果
time.sleep(10)
finally:
# 关闭浏览器
automation.close_browser()
4、编写shell脚本
@echo off
chcp 65001
REM 设置Python脚本路径
set SCRIPT_PATH=D:\metabase_automation.py
REM 调用Python脚本
python %SCRIPT_PATH%
REM 等待10秒
timeout /t 10 /nobreak
REM 关闭窗口
exit
5、创建任务计划程序