js 获取某标签下的所有标签的属性值

def get_node_attr_all(self, node):
    # 获取标签下的所有标签的属性
    attrs = self.driver.execute_script('''
        var nodes = [];
        var items = "";
        function fds(node) { //递归获取标签下的所有子标签
          var children = node.childNodes;
          for (var i = 0; i < children.length; i++) {
            if(children[i].nodeType==1){
                nodes.push(children[i]);
            }
           fds(children[i])
          }
        }
        fds(arguments[0]);
        for (var i=1; i< nodes.length;i++){ // 获取标签的属性值
            node = nodes[i]
            console.log(node.attributes)
            for (index = 0; index < node.attributes.length; ++index) {
                attr = node.attributes[index]
                items += attr.value+" " 
            }
        }
        return items;
     ''', node).lower()
    return attrs
node= driver.find_element(By.XPATH, '//ul/li')
attrs = self.get_node_attr_all(node)
def judge_element_exist(self, js_path, type=1, params=None):
    # 判断标签是否存在
    if type == 1:
        # 元素路径需要双引号包裹
        js_path = js_path.replace('"', "'")
        if params:
            result = self.driver.execute_script(
                '''var temp = arguments[0].querySelector("%s");if(temp){return true}else{return false}''' % js_path,
                params)
        else:
            result = self.driver.execute_script(
                '''var temp = document.querySelector("%s");if(temp){return true}else{return false}''' % js_path)
    else:
        # 元素路径需要单引号包裹 模糊匹配
        js_path = js_path.replace("'", '"')
        if params:
            result = self.driver.execute_script(
                '''var temp = arguments[0].querySelector('%s'); if(temp){return true}else{return false}''' % js_path,
                params)
        else:
            result = self.driver.execute_script(
                '''var temp = document.querySelector('%s');if(temp){return true}else{return false}''' % js_path)
    return result
def js_element_click(self, driver, js_path, type=1):
    # js 元素点击
    if type == 1:
        # 元素路径需要双引号包裹
        driver.execute_script('''document.querySelector("%s").click()''' % js_path)
    else:
        # 元素路径需要单引号包裹(模糊匹配)
        driver.execute_script('''document.querySelector('%s').click()''' % js_path)
def get_node_attr_one(self, node):
    # 获取单个标签的属性名
    attrs = self.driver.execute_script('''var items = "";
                    for (index = 0; index < arguments[0].attributes.length; ++index) {
                    items += arguments[0].attributes[index].name+" " };
                    return items;''', node).lower()
    return attrs

你可能感兴趣的:(javascript,前端,vue.js)