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
def judge_element_text(self, driver, tag_path, text): # js 根据文本定位 # tag_path div,span 单个标签名 或者路径 div>div.message>span js0 = """ var temp = Array.from(document.querySelectorAll('%s')).find(el => el.textContent.includes('%s')); if(temp){return true}else{return false}; """ % (tag_path, text) result = driver.execute_script(js0) return result def judge_element_text_click(self, driver, tag_path, text): # js 根据文本定位 js0 = """ var temp = Array.from(document.querySelectorAll('%s')).find(el => el.textContent.includes('%s')); if(temp){return temp.click()}; """ % (tag_path, text) result = driver.execute_script(js0) return result
文章来源地址https://www.toymoban.com/news/detail-529148.html
文章来源:https://www.toymoban.com/news/detail-529148.html
到了这里,关于js 获取某标签下的所有标签的属性值的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!