Files
auto_test/main.py
2025-11-06 11:59:35 +08:00

88 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
from re import T
from DrissionPage import Chromium
import requests
# 发送请求告知服务器执行完成
def task_push(project_name: str, browser_id: str, status: int = 200):
print('发送信息告知服务器脚本执行完成')
url = 'http://127.0.0.1:6060/task/info/push'
payload = {
"project_name": project_name,
"browser_id": browser_id,
"status": status
}
res = requests.post(url, json=payload).json()
print(res)
def main(http: str, browser_id: str):
"""
使用给定的 http 句柄接管已打开的指纹浏览器并执行示例登录流程
Args:
http (str): 浏览器的 http 句柄字符串
browser_id (str): 浏览器 ID
Returns:
None
"""
project_name = 'auto_test'
# 启动或接管浏览器,并获取标签页对象
print(f'使用 http 接管浏览器: {http}')
tab = Chromium(http).new_tab()
# 跳转到登录页面
tab.get('https://baidu.com')
# 定位到账号文本框,获取文本框元素
# ele = tab.ele('#user_login')
# # 输入账号
# ele.input('您的账号')
# # 定位到密码文本框并输入密码
# tab.ele('#user_password').input('您的密码')
# # 点击登录按钮
# tab.ele('@value=登 录').click()
# while True:
# print(1)
# time.sleep(5)
print(f'打开成功')
time.sleep(random.randint(5, 10))
task_push(project_name, browser_id)
if __name__ == '__main__':
"""
脚本入口:支持从命令行参数或环境变量读取 http 和 browser_id 并调用 main
- 命令行python project/test/log.py <http>
- 环境变量:
- BROWSER_HTTP=<http> python project/test/log.py
- BROWSER_ID=<browser_id> python project/test/log.py (可选,用于提交项目状态)
"""
import os
import sys
import time
http = os.getenv('BROWSER_HTTP') or (sys.argv[1] if len(sys.argv) > 1 else None)
if not http:
print('缺少 http 参数:请通过命令行传入或设置环境变量 BROWSER_HTTP')
sys.exit(1)
# 获取浏览器ID用于提交项目状态
browser_id = os.getenv('BROWSER_ID') or (sys.argv[2] if len(sys.argv) > 2 else None)
if browser_id:
print(f'接收到 browser_id: {browser_id}')
print(f'接收到 http: {http}')
try:
print(f'http --> {http}')
print(f'browser_id --> {browser_id}')
main(http, browser_id)
print('脚本执行完成')
# 这里可以使用 browser_id 来提交项目状态
# if browser_id:
# send_complete(browser_id)
except Exception as e:
print(f'脚本执行异常: {e}')
sys.exit(2)