findtask.py 2.85 KB
# -*- coding: utf-8 -*-
# @Time    :  2020/12/17 9:15
# @Author  :  Young Lee
# @Email   :  young_lee2017@163.com

from jumppage import jump_page
from selenium.webdriver.common.keys import Keys
import time


def get_subtasks_id(driver, common_xpath):
    """获取子任务ID列表

    :return: 返回子任务ID列表,不存在子任务时返回'notaskinfo'
    """
    expanded_bar = f"{common_xpath}/../preceding-sibling::td[2]/div/div"
    driver.find_element_by_xpath(expanded_bar).click()
    time.sleep(1)
    exist_subtask = driver.find_element_by_xpath(
        f"{common_xpath}/../../following-sibling::tr[1]/td/div[1]"
    ).get_attribute("class")
    if exist_subtask == "notaskinfo":
        return exist_subtask

    subtasks_id = []
    subtasks_id_ele = driver.find_elements_by_xpath(
        f"{common_xpath}/../../following-sibling::tr[1]/td//span[@class='label'][text()='任务ID']/following-sibling::span[1]")
    for subtask_id_ele in subtasks_id_ele:
        subtasks_id.append(subtask_id_ele.text)

    driver.find_element_by_xpath(expanded_bar).click()
    return subtasks_id


def find_task(driver, task_name):
    """定位任务位置

    :return: 找不到任务时返回None; 找到大任务但其下无子任务时返回"notaskinfo"; 找到大任务并其下有子任务时返回子任务ID列表
    """
    jump_page(driver, "任务管理", "任务设置")
    input_ele = driver.find_element_by_css_selector("input[placeholder='请输入任务名称']")
    input_ele.send_keys(Keys.CONTROL, 'a')
    input_ele.send_keys(task_name)
    driver.find_element_by_xpath("//div[@class='content']/div[1]//span[text()='查询']").click()
    time.sleep(1)

    common_xpath = f"//div[@class='content']/div[2]//tbody/tr/td[3]/div[text()='{task_name}']"
    num_of_pages = int(driver.find_element_by_css_selector(".el-pager>li:nth-last-child(1)").text)
    for num in range(num_of_pages):
        if num > 0:
            driver.find_element_by_css_selector(f".el-pager>li:nth-child({num+1})").click()
            time.sleep(1)

        find_result_check = driver.find_element_by_css_selector(".el-table__body-wrapper>*:nth-last-child(1)").get_attribute("class")
        if find_result_check == "el-table__empty-block":
            continue

        find_out_task_eles = driver.find_elements_by_css_selector(".content>div:nth-child(2) tbody>tr>td:nth-child(3)>div")
        find_out_tasks = [ele.get_attribute("textContent") for ele in find_out_task_eles]
        if task_name in find_out_tasks:
            return get_subtasks_id(driver, common_xpath)
        else:
            print(f"第{num+1}页: 未找到 '{task_name}'")
            if num + 1 == num_of_pages:
                return
            else:
                continue


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

    login_fx(driver, login_url, user, passwd)
    print(find_task(driver, "测试2021"))
    driver.quit()