获取点击路径的方法

机构树如图:
获取点击路径的方法

获取点击路径的方法

获取点击路径的方法

获取点击路径的方法

已知最小代理点机构的机构代码,以及他直系上属机构的机构代码,但是在未点开每一层级的展开按钮获取点击路径的方法 的情况下,网页源码中未加载出最小代理点机构的信息。因此我们需要便利这棵树,找到每个能展开的分支按钮获取点击路径的方法 。保存路径并存入字典 work_dict 中。

上代码:

@script_func
    def construct_dict(self):
        self.ie_driver.switch_to.frame('mainFrame')
        self.ie_driver.switch_to.frame('companyTreeLeft')
        level1 = self.ie_driver.find_elements_by_xpath("//li[@class='level1']")
        time.sleep(1)
        for v1_li in level1:
            v1_title = v1_li.find_element_by_xpath("a").get_attribute("title")
            if "分公司" in v1_title:
                v1_code = v1_title.split("-")[0]
                print(v1_code, v1_title)
                self.work_dict[v1_code] = None # 取代码
                # 点击一级展开按钮
                v1_li.find_element_by_xpath("button").send_keys(Keys.ENTER)
                self.ie_driver.implicitly_wait(30)  # 隐性等待,最长等30秒
                self.loop_work(v1_li, v1_code)
            with open("work_dict.json", "w", encoding="utf-8") as f:
                json.dump(self.work_dict, f)

    def loop_work(self, last_li, last_code):
        '''
        循环层
        :param last_li: 上一层li
        :param last_code: 上一层代码
        :return:
        '''
        cur_level = last_li.find_elements_by_xpath("ul/li")
        for cur_li in cur_level:
            btn_class = cur_li.find_element_by_xpath("button").get_attribute("class")
            cur_title = cur_li.find_element_by_xpath("a").get_attribute("title")
            print(cur_title)
            if btn_class.endswith("close"):
                cur_code = cur_title.split("-")[0]
                self.work_dict[cur_code] = last_code
                cur_li.find_element_by_xpath("button").send_keys(Keys.ENTER)
                self.ie_driver.implicitly_wait(30)  # 隐性等待,最长等30秒
                self.loop_work(cur_li, cur_code)
            elif btn_class.endswith("docu"):
                print(btn_class)
            else:
                with open("error.log", "a",encoding="uft-8") as f:
                    f.write("{} find".format(btn_class))
                print("{} find".format(btn_class))

其中 script_func 为登录修饰器

# 登录修饰器
def script_func(func):
    def wrapper(*args):
        self = args[0]
        # IE打开目标页面
        self.ie_driver.get(url_path)
        self.ie_driver.maximize_window()

        # 登录
        self.login()
        time.sleep(2)

        # 进入模块
        self.enter_left()
        return func(*args)
    return wrapper

执行结果:
生成 work_dict 字典,并保存为了一个 json 文件work_dict.json , 形如

获取点击路径的方法

下面我们拿到已知最小代理点机构的机构代码,以及他直系上属机构的机构代码后,便可通过这个字典获取需要点击展开按钮的路径。
代码如下:

    def get_path(self):
        click_path = []
        up_org_code = self.up_org_code  # 上级机构代码赋值
        while up_org_code:
            click_path.insert(0, up_org_code)
            up_org_code = self.work_dict[up_org_code]
        return click_path

拿到点击路劲后,就是正常的点击执行啦。

    def click_path(self):
        """
        点击路径
        :param path: 路径
        :return:
        """
        self.ie_driver.switch_to.frame('mainFrame')
        self.ie_driver.switch_to.frame('companyTreeLeft')
        self.ie_driver.implicitly_wait(30)  # 隐性等待,最长等30秒
        path = self.get_path()  # 调用get_path获取路径
        # 下面根据路劲点点点
        title = path.pop(0)  # 弹出路径
        cur_li = None
        level = self.ie_driver.find_elements_by_xpath("//li[@class='level1']")
        for v_li in level:
            if v_li.find_element_by_xpath("a").get_attribute("title").split("-")[0] == title:
                cur_li = v_li
                break
        cur_li.find_element_by_xpath("button").send_keys(Keys.ENTER)
        while path:
            title = path.pop(0)
            level = cur_li.find_elements_by_xpath("ul/li")
            for v_li in level:
                if v_li.find_element_by_xpath("a").get_attribute("title").split("-")[0] == title:
                    cur_li = v_li
                    break
            cur_li.find_element_by_xpath("button").send_keys(Keys.ENTER)
        level = cur_li.find_elements_by_xpath("ul/li")
        li_a = None
        for li in level:
            li_a = li.find_element_by_xpath("a")
            if li_a.get_attribute("title").split("-")[0] == self.new_org_title:
                print("title", li_a.get_attribute("title"))
                # li_a.find_element_by_xpath("button").send_keys(Keys.ENTER)
                li_a.send_keys(Keys.ENTER)
                break
        print("ok")
        self.ie_driver.switch_to.parent_frame()
        self.enter_tree_right()  # 进入右边frame后的操作
        self.ie_driver.switch_to.frame('companyTreeLeft')
        li_a.send_keys(Keys.ENTER)
        self.ie_driver.switch_to.parent_frame()
        self.enter_tree_right2()   #再次进入右边frame后的操作