You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
6.0 KiB
170 lines
6.0 KiB
from playwright.sync_api import Playwright, sync_playwright
|
|
from Ylt.CommonUtil import *
|
|
|
|
|
|
# 切换到节目
|
|
def changeJieMu(page):
|
|
# 点击“节目”
|
|
print("点击节目...")
|
|
program_library = page.get_by_text("节目").nth(0) # 通过 text 定位媒体库
|
|
program_library.click()
|
|
# 等待
|
|
wait()
|
|
|
|
|
|
# 排程
|
|
def paiCheng(page, keyword, stHour, stMin, stSec, endHour, endMin, endSec):
|
|
pc_library = page.get_by_text("排程").nth(0)
|
|
pc_library.click()
|
|
|
|
# 等待
|
|
wait()
|
|
# 定位目标 <span> 元素
|
|
today_span = page.locator("span.mat-button-toggle-label-content", has_text="日")
|
|
today_span.click()
|
|
# 等待
|
|
wait()
|
|
|
|
# 定位目标 <mat-icon> 元素
|
|
add_icon = page.locator("mat-icon", has_text="add").nth(1)
|
|
|
|
# 点击 <mat-icon> 元素
|
|
if add_icon.count() > 0: # 确保元素存在
|
|
add_icon.click()
|
|
# 定位目标 <span> 元素
|
|
broadcast_span = page.locator("span.mat-radio-label-content", has_text="插播")
|
|
|
|
# 点击 <span> 元素
|
|
if broadcast_span.count() > 0: # 确保元素存在
|
|
broadcast_span.click()
|
|
# 等待
|
|
wait()
|
|
|
|
# 点击 + 号
|
|
add_icon = page.locator("mat-icon", has_text="add").nth(2)
|
|
add_icon.click()
|
|
# 等待
|
|
wait()
|
|
|
|
# 等待目标对话框出现
|
|
dialog = page.get_by_role('dialog', name='选择节目') # 通过对话框标题定位
|
|
dialog.wait_for(state="visible")
|
|
|
|
# 在对话框内操作
|
|
# 定位所有 <span> 元素
|
|
spans = dialog.locator("span")
|
|
|
|
# 获取所有 <span> 元素的文本内容
|
|
text_list = spans.evaluate_all("elements => elements.map(span => span.textContent.trim())")
|
|
|
|
# 找到第一个以 keyword 开头的 <span> 元素
|
|
for index, text in enumerate(text_list):
|
|
if text.startswith(keyword):
|
|
print(f"Found first span with text: {text}")
|
|
# 获取元素的句柄并强制点击
|
|
span_handle = spans.nth(index).element_handle()
|
|
span_handle.evaluate("element => element.click()")
|
|
break # 找到第一个后退出循环
|
|
|
|
for index, text in enumerate(text_list):
|
|
if text.startswith("确认"):
|
|
print(f"Found first span with text: {text}")
|
|
span_handle = spans.nth(index).element_handle()
|
|
span_handle.evaluate("element => element.click()")
|
|
break
|
|
wait()
|
|
|
|
# 定位目标滑动开关的“滑块”部分
|
|
toggle_thumb = page.locator("div.mat-slide-toggle-thumb").nth(0)
|
|
# 点击“滑块”部分
|
|
toggle_thumb.click()
|
|
wait()
|
|
|
|
# 输入开始时间
|
|
input_element = page.locator("input#mat-input-3") # 通过 id 定位
|
|
input_element.click()
|
|
wait()
|
|
|
|
# 定位目标 <input> 元素
|
|
hour_input = page.locator("input.owl-dt-timer-input").nth(0) # 第一个输入框(小时)
|
|
minute_input = page.locator("input.owl-dt-timer-input").nth(1) # 第二个输入框(分钟)
|
|
second_input = page.locator("input.owl-dt-timer-input").nth(2) # 第三个输入框(秒)
|
|
# 输入时间
|
|
hour_input.fill(stHour) # 输入小时
|
|
wait()
|
|
minute_input.fill(stMin) # 输入分钟
|
|
wait()
|
|
second_input.fill(stSec) # 输入秒
|
|
wait()
|
|
# 设定
|
|
set_button = page.locator("button.owl-dt-control-button", has_text="Set")
|
|
set_button.click()
|
|
wait()
|
|
|
|
# ========================================== 输入结束时间==========================================
|
|
time_input = page.locator("input#mat-input-4") # 通过 id 定位
|
|
time_input.click()
|
|
wait()
|
|
|
|
hour_input = page.locator("input.owl-dt-timer-input").nth(0) # 第一个输入框(小时)
|
|
minute_input = page.locator("input.owl-dt-timer-input").nth(1) # 第二个输入框(分钟)
|
|
second_input = page.locator("input.owl-dt-timer-input").nth(2) # 第三个输入框(秒)
|
|
|
|
# 输入时间
|
|
hour_input.fill(endHour) # 输入小时
|
|
wait()
|
|
minute_input.fill(endMin) # 输入分钟
|
|
wait()
|
|
second_input.fill(endSec) # 输入秒
|
|
wait()
|
|
|
|
# 定位 <button> 元素
|
|
set_button = page.locator("button.owl-dt-control-button", has_text="Set")
|
|
set_button.click()
|
|
wait()
|
|
|
|
# 定位目标 <span> 元素
|
|
confirm_span = page.locator("span.mat-button-wrapper", has_text="确认")
|
|
# 点击 <span> 元素
|
|
confirm_span.click()
|
|
wait()
|
|
|
|
# 定位目标 <span> 元素
|
|
apply_span = page.locator("span", has_text="应用").nth(0)
|
|
apply_span.click()
|
|
wait()
|
|
|
|
|
|
def run(playwright: Playwright) -> None:
|
|
# 启动浏览器,禁用无头模式
|
|
browser = playwright.chromium.launch(headless=HEADLESS)
|
|
context = browser.new_context()
|
|
page = context.new_page()
|
|
|
|
try:
|
|
# 登录
|
|
login(page)
|
|
|
|
# 排程
|
|
paiCheng(page, PREFIX_NAME, '12', '00', '00', '13', '00', '00')
|
|
|
|
|
|
except Exception as e:
|
|
print(f"操作过程中发生错误: {e}")
|
|
finally:
|
|
# 关闭浏览器
|
|
print("操作完成,关闭浏览器...")
|
|
context.close()
|
|
browser.close()
|
|
|
|
def doPaiCheng():
|
|
# 初始化 Playwright
|
|
playwright = sync_playwright().start()
|
|
|
|
# 运行测试
|
|
run(playwright)
|
|
|
|
# 结束 Playwright
|
|
playwright.stop()
|
|
|