|
|
|
@ -1,6 +1,16 @@
|
|
|
|
|
import time
|
|
|
|
|
from playwright.sync_api import Playwright, sync_playwright
|
|
|
|
|
|
|
|
|
|
# 素材的固定文件夹名称
|
|
|
|
|
MATERIAL_FOLDER_NAME = '播报充电情况'
|
|
|
|
|
# 用户名
|
|
|
|
|
USERNAME = 'Ylt5786'
|
|
|
|
|
# 密码
|
|
|
|
|
PASSWORD = 'DJCtjy064'
|
|
|
|
|
# 前缀
|
|
|
|
|
PREFIX_NAME = 'Test'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 登录
|
|
|
|
|
def login(page):
|
|
|
|
|
# 打开登录页面
|
|
|
|
@ -10,12 +20,12 @@ def login(page):
|
|
|
|
|
# 输入用户名
|
|
|
|
|
print("输入用户名...")
|
|
|
|
|
username_input = page.locator("#mat-input-0") # 通过 id 定位用户名输入框
|
|
|
|
|
username_input.fill("Ylt5786")
|
|
|
|
|
username_input.fill(USERNAME)
|
|
|
|
|
|
|
|
|
|
# 输入密码
|
|
|
|
|
print("输入密码...")
|
|
|
|
|
password_input = page.locator("input[type='password']") # 根据实际元素定位密码输入框
|
|
|
|
|
password_input.fill("DJCtjy064")
|
|
|
|
|
password_input.fill(PASSWORD)
|
|
|
|
|
|
|
|
|
|
# 点击登录按钮
|
|
|
|
|
print("点击登录按钮...")
|
|
|
|
@ -26,14 +36,12 @@ def login(page):
|
|
|
|
|
print("等待登录成功...")
|
|
|
|
|
page.wait_for_url("https://ww1.colorlightcloud.com/home", timeout=60000) # 等待跳转到 home 页面
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 删除节目
|
|
|
|
|
def delProgram(page,keyword):
|
|
|
|
|
def delProgram(page, keyword):
|
|
|
|
|
# 点击“节目”
|
|
|
|
|
print("点击节目...")
|
|
|
|
|
program_library = page.get_by_text("节目").nth(0) # 通过 text 定位媒体库
|
|
|
|
|
program_library.click()
|
|
|
|
|
changeJieMu(page)
|
|
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
# 定位所有符合条件的 <div> 元素
|
|
|
|
|
divs = page.locator("div.content-title")
|
|
|
|
|
|
|
|
|
@ -41,14 +49,16 @@ def delProgram(page,keyword):
|
|
|
|
|
text_list = divs.evaluate_all("elements => elements.map(div => div.textContent.trim())")
|
|
|
|
|
|
|
|
|
|
# 遍历文本内容,找到包含 keyword 的元素
|
|
|
|
|
for index, text in enumerate(text_list):
|
|
|
|
|
cnt = 0
|
|
|
|
|
# 从后往前遍历
|
|
|
|
|
for index in range(len(text_list) - 1, -1, -1):
|
|
|
|
|
text = text_list[index]
|
|
|
|
|
if keyword in text:
|
|
|
|
|
print(f"Found element with text: {text}")
|
|
|
|
|
#点击
|
|
|
|
|
# 点击
|
|
|
|
|
divs.nth(index).click()
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
# 点击删除按钮
|
|
|
|
|
print(page.locator("mat-icon:has-text('delete')").count())
|
|
|
|
|
delete_icon = page.locator("mat-icon:has-text('delete')").nth(0)
|
|
|
|
|
if delete_icon.is_visible():
|
|
|
|
|
delete_icon.click() # 点击该元素
|
|
|
|
@ -57,6 +67,13 @@ def delProgram(page,keyword):
|
|
|
|
|
confirm_button = page.locator("button:has-text('确定')")
|
|
|
|
|
if confirm_button.is_visible():
|
|
|
|
|
confirm_button.click() # 点击该元素
|
|
|
|
|
cnt = cnt + 1
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
if cnt > 0:
|
|
|
|
|
print("删除" + str(cnt) + "个节目")
|
|
|
|
|
else:
|
|
|
|
|
print("没有找到需要删除的节目,前缀:" + keyword)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 删除以前上传的素材
|
|
|
|
|
def delMaterial(page):
|
|
|
|
@ -66,9 +83,9 @@ def delMaterial(page):
|
|
|
|
|
media_library.click()
|
|
|
|
|
|
|
|
|
|
# 先删除历史文件,防止垃圾太多
|
|
|
|
|
# 点击 "播报充电情况"
|
|
|
|
|
print("点击播报充电情况...")
|
|
|
|
|
playback_charge_status = page.get_by_text("播报充电情况")
|
|
|
|
|
# 点击 MATERIAL_FOLDER_NAME
|
|
|
|
|
print("点击" + MATERIAL_FOLDER_NAME + "...")
|
|
|
|
|
playback_charge_status = page.get_by_text(MATERIAL_FOLDER_NAME)
|
|
|
|
|
playback_charge_status.dblclick()
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
# 检查 <span> 元素是否存在
|
|
|
|
@ -94,10 +111,9 @@ def delMaterial(page):
|
|
|
|
|
confirm_button = page.locator("button:has-text('确定')")
|
|
|
|
|
if confirm_button.is_visible():
|
|
|
|
|
confirm_button.click() # 点击该元素
|
|
|
|
|
else:
|
|
|
|
|
print("<span> 元素未找到")
|
|
|
|
|
else:
|
|
|
|
|
print("未找到 <span> 元素,可能页面未加载或元素不存在")
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
print("删除素材完成!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 上传文件
|
|
|
|
|
def uploadVideo(page):
|
|
|
|
@ -111,9 +127,9 @@ def uploadVideo(page):
|
|
|
|
|
media_library = page.get_by_text("媒体库") # 通过 text 定位媒体库
|
|
|
|
|
media_library.click()
|
|
|
|
|
|
|
|
|
|
# 点击 "播报充电情况"
|
|
|
|
|
print("点击播报充电情况...")
|
|
|
|
|
playback_charge_status = page.get_by_text("播报充电情况").nth(0)
|
|
|
|
|
# 点击 MATERIAL_FOLDER_NAME
|
|
|
|
|
print("点击" + MATERIAL_FOLDER_NAME + "...")
|
|
|
|
|
playback_charge_status = page.get_by_text(MATERIAL_FOLDER_NAME).nth(0)
|
|
|
|
|
playback_charge_status.dblclick()
|
|
|
|
|
|
|
|
|
|
# 点击“上传”按钮
|
|
|
|
@ -136,16 +152,17 @@ def uploadVideo(page):
|
|
|
|
|
print("上传进行中...")
|
|
|
|
|
time.sleep(0.5) # 每隔 0.5 秒检查一次
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 发布节目
|
|
|
|
|
def publish(page):
|
|
|
|
|
print("点击编辑节目...")
|
|
|
|
|
jiemu_library = page.get_by_text("编辑节目") # 通过 text 定位编辑节目
|
|
|
|
|
jiemu_library.click()
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
# 点击 "播报充电情况"
|
|
|
|
|
print("点击播报充电情况...")
|
|
|
|
|
playback_charge_status = page.locator("span:has-text('播报充电情况')").nth(0) # 定位 <span> 元素
|
|
|
|
|
# 点击 MATERIAL_FOLDER_NAME
|
|
|
|
|
print("点击" + MATERIAL_FOLDER_NAME + "...")
|
|
|
|
|
playback_charge_status = page.locator("span:has-text('" + MATERIAL_FOLDER_NAME + "')").nth(0) # 定位 <span> 元素
|
|
|
|
|
if playback_charge_status.is_visible():
|
|
|
|
|
playback_charge_status.click() # 点击该元素
|
|
|
|
|
# 点击 <img> 元素
|
|
|
|
@ -186,20 +203,32 @@ def publish(page):
|
|
|
|
|
publish_button = page.locator("div.btn.publish") # 定位 <div> 元素
|
|
|
|
|
if publish_button.is_visible():
|
|
|
|
|
publish_button.click() # 点击该元素
|
|
|
|
|
# 定位并点击 <mat-tree-node> 元素
|
|
|
|
|
print("定位并点击 <mat-tree-node> 元素...")
|
|
|
|
|
tree_node = page.locator(
|
|
|
|
|
"mat-tree-node.node-custom-style:has-text('充电站')") # 定位 <mat-tree-node> 元素
|
|
|
|
|
if tree_node.is_visible():
|
|
|
|
|
tree_node.click() # 点击该元素
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
tree_node = page.locator("mat-tree-node.node-custom-style", has_text="充电站")
|
|
|
|
|
# 点击 <mat-tree-node> 元素
|
|
|
|
|
if tree_node.count() > 0: # 确保元素存在
|
|
|
|
|
tree_node.click()
|
|
|
|
|
print("Clicked on the mat-tree-node.")
|
|
|
|
|
# 点击确定按钮
|
|
|
|
|
# 定位目标 <span> 元素
|
|
|
|
|
confirm_button = page.locator("span.mat-button-wrapper", has_text="确认")
|
|
|
|
|
# 点击 <span> 元素
|
|
|
|
|
if confirm_button.count() > 0: # 确保元素存在
|
|
|
|
|
confirm_button.click()
|
|
|
|
|
print("Clicked on the confirm button.")
|
|
|
|
|
else:
|
|
|
|
|
print("Confirm button not found.")
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print("<mat-tree-node> 元素未找到")
|
|
|
|
|
else:
|
|
|
|
|
print("<div> 元素未找到")
|
|
|
|
|
else:
|
|
|
|
|
print("<img> 元素未找到")
|
|
|
|
|
else:
|
|
|
|
|
print("<span> 元素未找到")
|
|
|
|
|
print("mat-tree-node not found.")
|
|
|
|
|
|
|
|
|
|
# 切换到节目
|
|
|
|
|
def changeJieMu(page):
|
|
|
|
|
# 点击“节目”
|
|
|
|
|
print("点击节目...")
|
|
|
|
|
program_library = page.get_by_text("节目").nth(0) # 通过 text 定位媒体库
|
|
|
|
|
program_library.click()
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
def run(playwright: Playwright) -> None:
|
|
|
|
|
# 启动浏览器,禁用无头模式
|
|
|
|
@ -212,19 +241,21 @@ def run(playwright: Playwright) -> None:
|
|
|
|
|
login(page)
|
|
|
|
|
|
|
|
|
|
# 只删除固定前缀的节目
|
|
|
|
|
delProgram(page,'Playlist')
|
|
|
|
|
delProgram(page, PREFIX_NAME)
|
|
|
|
|
|
|
|
|
|
# 删除素材
|
|
|
|
|
# delMaterial(page)
|
|
|
|
|
delMaterial(page)
|
|
|
|
|
|
|
|
|
|
# 上传文件
|
|
|
|
|
#uploadVideo(page)
|
|
|
|
|
uploadVideo(page)
|
|
|
|
|
|
|
|
|
|
# 发布节目
|
|
|
|
|
#publish(page)
|
|
|
|
|
publish(page)
|
|
|
|
|
|
|
|
|
|
time.sleep(60)
|
|
|
|
|
# 切换到节目
|
|
|
|
|
changeJieMu(page)
|
|
|
|
|
|
|
|
|
|
time.sleep(60)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"操作过程中发生错误: {e}")
|
|
|
|
@ -242,4 +273,4 @@ playwright = sync_playwright().start()
|
|
|
|
|
run(playwright)
|
|
|
|
|
|
|
|
|
|
# 结束 Playwright
|
|
|
|
|
playwright.stop()
|
|
|
|
|
playwright.stop()
|
|
|
|
|