deletetask.py 3.61 KB
# -*- coding: utf-8 -*-
# @Time    :  2021/1/4 22:05
# @Author  :  Young Lee
# @Email   :  young_lee2017@163.com

from findtask import find_task
from monitor_subtask_status import monitor_subtask_status
import time


def delete_task(driver, task_name, monitor_times=30):
    """删除大任务

    :param task_name: 大任务名称
    :param monitor_times: 最大检测次数,默认为30
    :return: 删除成功返回None, 删除失败返回task_name
    """
    subtasks_id = find_task(driver, task_name)
    if isinstance(subtasks_id, list):
        delete_subtask(driver, task_name, monitor_times=monitor_times, subtasks_id=subtasks_id)
    elif not subtasks_id:
        print(f"任务 '{task_name}' 不存在, 无需删除!")
        return

    # 删除大任务
    driver.find_element_by_xpath(f"//div[text()='{task_name}']/../following-sibling::td[last()]//span[contains(@class, "
                                 f"'el-icon-delete')]").click()
    driver.find_element_by_xpath("//div[@aria-label='提示']//button/span[contains(text(), '确定')]").click()
    check_task = find_task(driver, task_name)

    return task_name if check_task else None


def delete_subtask(driver, task_name, subtasks_list=None, monitor_times=30, subtasks_id=None):
    """删除子任务

    :param task_name: 大任务名称
    :param subtasks_list: 要删除子任务名称列表
    :param monitor_times: 最大检测次数,默认为30
    :param subtask_id: 大任务下子任务ID列表,为None时则自动收集
    :return: 返回未删掉的任务列表
    """
    rest_subtasks = []
    subtasks_id = subtasks_id if subtasks_id else find_task(driver, task_name)
    if not subtasks_id:
        print("未找到任何任务!")
        return

    # 打开‘修改视频源配置’
    driver.find_element_by_xpath(f"//div[text()='{task_name}']/../following-sibling::td[last()]//span[contains(@class,"
                                 f"'el-icon-video-camera-solid')]").click()

    # 删除列表中存在的任务
    existing_eles = driver.find_elements_by_css_selector("div[aria-label='视频源配置修改'] tbody>tr")
    for i in range(len(existing_eles)-1, -1, -1):
        existing_ele = existing_eles[i]
        subtask_name = existing_ele.find_element_by_css_selector("td:nth-child(1)>div>div").get_attribute("textContent").strip()
        if not subtasks_list or subtask_name in subtasks_list:
            print(f"删除 '{subtask_name}'")
            existing_ele.find_element_by_css_selector("td:nth-last-child(1)>div>.el-icon-delete").click()
            driver.find_element_by_xpath("//div[@class='el-message-box']//button/span[contains(text(),'确定')]").click()
            time.sleep(0.5)

    driver.find_element_by_xpath("//div[@aria-label='视频源配置修改']/div[@class='el-dialog__footer']/span/button/"
                                 "span[text()='确 定']").click()

    for i in range(monitor_times):
        print(f"第{i}次:")
        subtasks_status = monitor_subtask_status(driver, task_name)
        if subtasks_status:
            subtasks_name = [subtask[0] for subtask in subtasks_status]
            rest_subtasks = [name for name in subtasks_list if name in subtasks_name] if subtasks_list else subtasks_name
        else:
            rest_subtasks = []

        if not rest_subtasks:
            break
        time.sleep(2)

    return rest_subtasks


if __name__ == '__main__':
    from unitinit import *

    login_fx(driver, login_url, user, passwd)
    task_name = "测试"
    subtask_list = ["100辆车"]
    print(delete_task(driver, task_name))
    # print(delete_subtask(driver, task_name, subtask_list))
    driver.quit()