dataretrieval.py 3.29 KB
# -*- coding: utf-8 -*-
# @Time    :  2020/12/22 9:12
# @Author  :  Young Lee
# @Email   :  young_lee2017@163.com

from jumppage import jump_page
import time
import re


def select_condition(driver, conditopn_type, specific_value):
    """选择条件"""
    common_xpath = f"//label[@class='el-form-item__label'][text()='{conditopn_type}']"
    input_select = f"{common_xpath}/following-sibling::div[1]//input"
    # li_select = f"{common_xpath}/following-sibling::div[1]//li/span[text()='{specific_value}']"


    driver.find_element_by_xpath(input_select).click()  # 点击选择框
    time.sleep(0.5)
    li_eles = driver.find_elements_by_xpath(f"{common_xpath}/following-sibling::div[1]//li/span")
    li_dic = {ele.get_attribute('textContent'): ele for ele in li_eles}
    if specific_value not in li_dic.keys():
        return False
    li_dic[specific_value].click()
    return True
    # driver.find_element_by_xpath(li_select).click()  # 选择具体值


def vehicle_search(driver, task_name=None, subtask_name=None):
    """过车记录检索

    :param task_name: 大任务名称
    :param subtask_name: 子任务名称
    :return: {'first_page': <first page num>, 'total': <total num>}
    """
    jump_page(driver, "智能检索", "过车检索")
    if task_name:
        select_result = select_condition(driver, "任务名称", task_name)
        if not select_result:
            print(f"未找到大任务'{task_name}'")
            return f"未找到大任务'{task_name}'"
        if subtask_name:
            select_result = select_condition(driver, "视频名称", subtask_name)
            if not select_result:
                print(f"未找到子任务'{subtask_name}'")
                return f"未找到子任务'{subtask_name}'"

    driver.find_element_by_xpath("//div[@class='innnerBox']/div[2]//button/span[text()='查询']").click()  # 点击查询

    # 等待查询数据出现,30S超时
    start = time.time()
    while True:
        style = driver.find_element_by_class_name("el-loading-mask").get_attribute('style')
        now = time.time()
        if style == 'display: none;':
            break
        elif (now - start) > 30:
            print("超时未检索到数据")
            return "超时未检索到数据"
        time.sleep(0.2)

    judge_name = driver.find_element_by_css_selector(".el-table__body-wrapper>*:nth-last-child(1)").get_attribute('class')
    if judge_name == 'el-table__empty-block':
        vehiclesnumber_of_firstpage = 0
    else:
        li_eles = driver.find_elements_by_css_selector("table[class='el-table__body']>tbody>tr")
        vehiclesnumber_of_firstpage = len(li_eles)
    total_ele = driver.find_element_by_css_selector("div[class='innnerBox']>div:nth-child(3) span[class='el-pagination__total']")
    # driver.execute_script("arguments[0].style.display='block'", total_ele)
    total_number = total_ele.get_attribute("textContent")
    vehiclesnumber_of_all = re.search("[0-9]+", total_number).group()
    search_result = {"first_page": vehiclesnumber_of_firstpage, "total": int(vehiclesnumber_of_all)}
    print(search_result)
    return search_result


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

    from login import login_fx
    login_fx(driver, login_url, user, passwd)
    vehicle_search(driver, '测试任务DOmxF', '大货车合并文件')
    driver.quit()