Javascript 在python/selenium中使用xpath搜索执行js代码失败
下面的JS代码(通过python selenium运行)搜索文档中的文本,定位标记并提取HTML元素的css属性Javascript 在python/selenium中使用xpath搜索执行js代码失败,javascript,python,xpath,selenium-webdriver,automated-tests,Javascript,Python,Xpath,Selenium Webdriver,Automated Tests,下面的JS代码(通过python selenium运行)搜索文档中的文本,定位标记并提取HTML元素的css属性 styleJSON = driver.execute_script("""function getElementByXpath(path){return document.evaluate(path, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);}function getPathTo(element) {
styleJSON = driver.execute_script("""function getElementByXpath(path){return document.evaluate(path, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);}function getPathTo(element) {if(element.id!=='') return 'id("'+element.id+'")';if (element===document.body) return '//'+element.tagName; var ix= 0;var siblings= element.parentNode.childNodes; for (var i= 0; i<siblings.length; i++) {var sibling= siblings[i];if (sibling===element)return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';if (sibling.nodeType===1 && sibling.tagName===element.tagName)ix++;}}var not_found=[],styleSheet=[],pos,temp;try{var corpus=JSON.parse(arguments[0]);console.log(corpus);for(var k = 0; k < corpus.length; k++){pos=0; elements = getElementByXpath('//*[normalize-space(text())=normalize-space('+cleanStringForXpath(corpus[k])+')]');elem=elements.iterateNext();if(!elem){not_found.push(corpus[k]);} while(elem) { pos++;temp = {'corpus': corpus[k], 'font_size':window.getComputedStyle(elem).fontSize, 'font_color':window.getComputedStyle(elem).color,'font_weight':window.getComputedStyle(elem).fontWeight,'font_family':window.getComputedStyle(elem).fontFamily,'tag': elem.tagName,'xpath':getPathTo(elem),'pos':pos,'text_decoration':window.getComputedStyle(elem).textDecoration}; if(temp.tag!=='SCRIPT' || temp.tag!=='TITLE'){styleSheet.push(temp);console.log(temp);}elem=elements.iterateNext();}}}catch(err){console.log(err);}console.log(not_found);return JSON.stringify({stylesheet:styleSheet,not_found: not_found});""", json.dumps(corpus, ensure_ascii=False, encoding='utf-8'))
styleJSON=driver.execute\u script(““”函数getElementByXpath(path){返回document.evaluate(path,document,null,XPathResult.ORDERED\u NODE\u ITERATOR\u TYPE,null);}函数getPathTo(element){if(element.id!='')返回'id('+element.id+');if(element==document.body)返回“/”+element.tagName;var ix=0;var sibbins=element.parentNode.childNodes;for(var i=0;i