Ejemplo n.º 1
0
class Query:
    search_button_xpath = "/html/body/div[3]/div[3]/div/div[2]/div[3]/a[3]"
    inventor_input_id = "tableSearchItemIdIVDB021"
    proposer_input_id = "tableSearchItemIdIVDB020"
    time_select_id = "IVDB012select"
    time_input_id = "tableSearchItemIdIVDB012"

    def __init__(self, progressController):
        self.__progressController = progressController
        self.__driver = progressController.getWebDriver()
        self.__waitEngine = WaitEngine(self.__driver)

    def queryTarget(self, inventor, proposer, startDate, patentTypeIndex):
        if self.__waitEngine.wait_for_loading():
            if self.__isElementLoadingSuccess():
                if self.__inputQueryTargetData(inventor, proposer, startDate,
                                               patentTypeIndex):
                    if self.__waitEngine.wait_for_loading():
                        self.__waitEngine.waitForSeconds(1)
                        pageSum = self.__getPageSum()
                        if pageSum is not None:
                            self.__progressController.queryTargetSuccessfully(
                                pageSum)
                            return True
                        else:
                            print("页码为零")
                            Config.writeLog("页码为零")
                            self.__progressController.queryTargetUnsuccessfully(
                            )
                            return False
                    else:
                        print("查询等待超时")
                        Config.writeLog("查询等待超时")
                        self.__progressController.queryTargetUnsuccessfully()
                        return False
                else:
                    print("查询失败")
                    Config.writeLog("查询失败")
                    self.__progressController.queryTargetUnsuccessfully()
                    return False
            else:
                print("元素未加载")
                Config.writeLog("元素未加载")
                self.__progressController.queryTargetUnsuccessfully()
                return False
        else:
            print("url加载超时")
            Config.writeLog("url加载超时")
            self.__progressController.queryTargetUnsuccessfully()
            return False

    # 元素是否加载完成
    def __isElementLoadingSuccess(self):
        try:
            search_button = self.__driver.find_element_by_xpath(
                Query.search_button_xpath)
            inventor_input = self.__driver.find_element_by_id(
                Query.inventor_input_id)
            proposer_input = self.__driver.find_element_by_id(
                Query.proposer_input_id)
            time_select = self.__driver.find_element_by_id(
                Query.time_select_id)
            time_input = self.__driver.find_element_by_id(Query.time_input_id)

            if search_button.is_displayed() and inventor_input.is_displayed(
            ) and proposer_input.is_displayed() and time_select.is_displayed(
            ) and time_input.is_displayed():
                return True
            else:
                print("元素没显示")
                Config.writeLog("元素没显示")
                return False
        except Exception as e:
            print("元素抛异常")
            Config.writeLog("元素抛异常")
            Config.writeException(e)
            return False

    def __inputQueryTargetData(self, inventor, proposer, startDate,
                               patentTypeIndex):
        try:
            # 填写发明人
            self.__driver.execute_script("document.getElementById(\"" +
                                         Query.inventor_input_id +
                                         "\").setAttribute(\"value\",\"" +
                                         inventor + "\")")
            Config.writeLog("发明人")
            # 填写申请人
            self.__driver.execute_script("document.getElementById(\"" +
                                         Query.proposer_input_id +
                                         "\").setAttribute(\"value\",\"" +
                                         proposer + "\")")
            Config.writeLog("申请人")
            # 点击时间的check_list
            self.__driver.execute_script(
                "document.getElementById(\"" + Query.time_select_id +
                "\").firstElementChild.firstElementChild.click();")
            WaitEngine.waitForSeconds(2)  # 等待两秒
            self.__driver.execute_script(
                "document.getElementById(\"" + Query.time_select_id +
                "\").firstElementChild.childNodes[2].childNodes[2].firstElementChild.click();"
            )
            Config.writeLog("点击时间")
            # 填写时间
            self.__driver.execute_script("document.getElementById(\"" +
                                         Query.time_input_id +
                                         "\").setAttribute(\"value\",\"" +
                                         startDate + "\")")
            Config.writeLog("填写时间")
            # 选择专利类型
            self.__choosePatentType(patentTypeIndex)
            Config.writeLog("专利类型")
            WaitEngine.waitForSeconds(3)  # 等待三秒
            # 点击检索按钮
            self.__driver.execute_script(
                "document.getElementsByClassName(\"box-content-bottom\").item(0).childNodes.item(5).click();"
            )
            Config.writeLog("点击按钮")
            return True
        except Exception as e:
            Config.writeException(e)
            print(e)
            return False

    # 选择专利类型
    def __choosePatentType(self, patentTypeIndex):
        for i in range(3):  # 去掉多余的active
            self.__driver.execute_script(
                "document.getElementsByName(\"inventiontype\").item(" +
                str(i) + ").classList.remove(\"active\");")
        self.__driver.execute_script(
            "document.getElementsByName(\"inventiontype\").item(" +
            str(patentTypeIndex) + ").classList.add(\"active\");")

    # 获取页码总数
    def __getPageSum(self):
        if self.__driver.page_source.find("没有检索到") != -1:
            print("没有检索到")
            return 0
        else:
            try:
                page_sum_str = self.__driver.execute_script(
                    "return document.getElementsByClassName(\"page_top\").item(0).childNodes.item(document.getElementsByClassName(\"page_top\").item(0).childNodes.length - 1).textContent;"
                )
                strTemp = page_sum_str[page_sum_str.find("共") + 1:-1]
                page_sum = int(strTemp[:strTemp.find("页")])
                return page_sum
            except Exception as e:
                Config.writeException(e)
                print(e)
                return None

    def changePage(self, pageIndex):
        try:
            self.__driver.execute_script(
                "document.getElementById(\"txt\").setAttribute(\"value\", " +
                str(pageIndex) + ");")
            self.__driver.execute_script(
                "document.getElementsByClassName(\"page_bottom\").item(0).childNodes.item(document.getElementsByClassName(\"page_bottom\").item(0).childNodes.length - 2).click();"
            )
            self.__progressController.changePageSuccessfully()
            return True
        except Exception as e:
            Config.writeException(e)
            print(e)
            self.__progressController.changePageUnsuccessfully()
            return False
Ejemplo n.º 2
0
class LawState:
    close_btn_string = "取消"

    def __init__(self, driver, itemCollection):
        self.wait_state = WaitEngine(driver)
        self.driver = driver
        self.__itemCollection = itemCollection

    # 点击法律信息的按钮
    def __click_law_state_button(self, which_item):
        # 法律信息
        try:
            self.driver.execute_script(
                "document.getElementsByClassName(\"item-footer\").item(" +
                str(which_item) +
                ").childNodes.item(1).childNodes.item(3).click();")
        except Exception as e:
            Config.writeException(e)
            print(e)
            try:
                self.driver.execute_script(
                    "document.getElementsByClassName(\"item-footer\").item(" +
                    str(which_item) +
                    ").childNodes.item(1).childNodes.item(3).click();")
            except Exception as e:
                print(e)
                Config.writeException(e)
        return

    # 等待法律信息框加载完成
    def __wait_for_law_state(self):
        if not self.wait_state.wait_for_loading():
            Config.writeLog("等待超时")
            print("等待超时")
            self.__itemCollection.collectingLawDataUnsuccessfully()

        if self.__wait_for_close_button():
            pass
        else:
            Config.writeLog("关闭按钮没出来")
            print("关闭按钮没出来")
            self.__itemCollection.collectingLawDataUnsuccessfully()

        if self.wait_state.query_result_state():
            pass
        else:
            Config.writeLog("加载异常")
            print("加载异常")
            self.__itemCollection.collectingLawDataUnsuccessfully(
            )  # TODO:添加加载失败的处理函数
        return

    def __wait_for_law_state_loading(self):
        if not self.wait_state.wait_for_loading():
            Config.writeLog("等待超时")
            print("等待超时")
            return False
            # self.__itemCollection.collectingLawDataUnsuccessfully()
        return True

    def __check_for_colse_button(self):
        if self.__wait_for_close_button():
            pass
        else:
            Config.writeLog("关闭按钮没出来")
            self.__itemCollection.collectingLawDataUnsuccessfully()

    def __check_if_lost(self):
        if self.wait_state.query_result_state():
            pass
        else:
            print("加载异常")
            Config.writeLog("加载异常")
            self.__itemCollection.collectingLawDataUnsuccessfully(
            )  # TODO:添加加载失败的处理函数

    # 等待关闭按钮加载完成
    def __wait_for_close_button(self):
        key = WaitEngine.generateOverTimeKey()
        while self.driver.page_source.find(self.close_btn_string) is -1:
            if WaitEngine.isOverTime(key, 2):
                return False
        return True

    # 采集日期最近的法律信息
    def __get_law_state(self):
        return self.driver.execute_script(
            "return document.getElementById(\"lawResult\").getElementsByTagName(\"td\").item(document.getElementById(\"lawResult\").getElementsByTagName(\"td\").length - 1).innerText;"
        )

    def __get_law_update(self):
        law_update = self.driver.execute_script(
            "return document.getElementById(\"lawResult\").children[1].children[document.getElementById(\"lawResult\").children[1].children.length-1].children[1].innerText;"
        )
        return law_update

    # 关闭法律信息框
    def __close_law_state(self):
        self.driver.execute_script(
            "document.getElementsByClassName(\"ui-dialog-close\").item(0).click();"
        )
        return

    def __hasLawItem(self):
        len = self.driver.execute_script(
            "return document.getElementById(\"lawResult\").getElementsByTagName(\"td\").length;"
        )
        if len > 0:
            return True
        else:
            return False

    def hasShowLawStateDialog(self):
        try:
            length = self.driver.execute_script(
                "return document.getElementsByClassName(\"ui-dialog\").length;"
            )
            if length > 0:
                return True
            else:
                return False
        except:
            return False

    def waitForLawStateDialog(self, whichItem):
        tryTimes = 0
        while not self.hasShowLawStateDialog():
            WaitEngine.waitForSeconds(2)
            self.__click_law_state_button(whichItem)
            tryTimes += 1
            if tryTimes > 3:
                break
        if tryTimes >= 4:
            return False
        else:
            return True

    def collectingLawState(self, whichItem):
        try:
            Config.writeLog("点击按钮")
            WaitEngine.waitForSeconds(1)
            self.__click_law_state_button(whichItem)
            WaitEngine.waitForSeconds(2)
            if self.waitForLawStateDialog(whichItem):
                if self.__wait_for_law_state_loading() is True:
                    self.__check_for_colse_button()
                    self.__check_if_lost()
                else:
                    self.__itemCollection.collectingLawDataUnsuccessfully()
                    return
            else:
                self.__itemCollection.collectingLawDataUnsuccessfully()
                return
            Config.writeLog("法律状态")
            law_state = self.__get_law_state()
            if law_state.find("无数据") == -1:
                Config.writeLog("法律日期")
                law_update = self.__get_law_update()
            else:
                law_update = "无数据"
            Config.writeLog("关闭按钮")
            self.__close_law_state()
            Config.writeLog("采集成功")
            self.__itemCollection.collectingLawDataSuccessfully(
                law_update, law_state)
        except Exception as e:
            print("采集异常")
            Config.writeLog("采集异常")
            Config.writeException(e)
            self.__itemCollection.collectingLawDataUnsuccessfully()

    #     TODO:处理点击失败的响应

    # 采集法律信息数据
    def collecting_law_state(self, which_item):
        try:
            Config.writeLog("点击按钮")
            WaitEngine.waitForSeconds(2)
            self.__click_law_state_button(which_item)
            if self.__wait_for_law_state_loading() is True:
                self.__check_for_colse_button()
                self.__check_if_lost()
            else:
                Config.writeLog("点击按钮")
                WaitEngine.waitForSeconds(2)
                self.__click_law_state_button(which_item)
                if self.__wait_for_law_state_loading() is True:
                    self.__check_for_colse_button()
                    self.__check_if_lost()
                else:
                    self.__itemCollection.collectingLawDataUnsuccessfully()
                    return
            Config.writeLog("法律状态")
            law_state = self.__get_law_state()
            if law_state.find("无数据") == -1:
                Config.writeLog("法律日期")
                law_update = self.__get_law_update()
            else:
                law_update = "无数据"
            Config.writeLog("关闭按钮")
            self.__close_law_state()
            Config.writeLog("采集成功")
            self.__itemCollection.collectingLawDataSuccessfully(
                law_update, law_state)
        except Exception as e:
            print("采集异常")
            Config.writeLog("采集异常")
            Config.writeException(e)
            self.__itemCollection.collectingLawDataUnsuccessfully()
        return