【问题标题】:Execution of js code with xpath search fails in python/selenium在 python/selenium 中使用 xpath 搜索执行 js 代码失败
【发布时间】:2017-02-05 19:56:04
【问题描述】:

下面的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) {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'))

我的问题是传入带有单引号和双引号的文本时。我尝试使用 link 中的函数 cleanStringForXpath 来清理传递给 xpath query'//*[normalize-space(text())=normalize-space('+cleanStringForXpath(corpus[k])+')]' 的文本,但不能让它工作。尝试在python中通过简单的连接"""..."""+"""..."""将它添加到execute_script,尝试将函数作为参数传递给execute_script,然后调用Function构造函数,尝试在python中使用%s修饰符,但这些都没有产生任何结果结果。这可能是什么问题?

【问题讨论】:

    标签: javascript python xpath selenium-webdriver automated-tests


    【解决方案1】:

    你可以用这个方法

    s = (
                                    """
                                    (function() {
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
                                        var bannerr = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        bannerr.click()
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
                                        var bannerr = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        bannerr.click()
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
                                        var bannerr = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        bannerr.click()
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
                                        var bannerr = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        bannerr.click()
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
                                        var bannerr = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        bannerr.click()
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
                                        var bannerr = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        bannerr.click()
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
                                        var bannerr = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        bannerr.click()
                                        var banner = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        banner.click()
    
    
                                    })()"""
                                    % (js_ile_gonderme[0], js_ile_gonderme[1], js_ile_gonderme[2], js_ile_gonderme[3], js_ile_gonderme[4], js_ile_gonderme[5], js_ile_gonderme[6], js_ile_gonderme[7], js_ile_gonderme[8], js_ile_gonderme[9], js_ile_gonderme[10], js_ile_gonderme[11], js_ile_gonderme[12], js_ile_gonderme[13], js_ile_gonderme[14]))
                                    try:
    
                                        driver.execute_script(s)
                                    except:
                                        pass
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    相关资源
    最近更新 更多