编程语言
首页 > 编程语言> > Javascript 使用xpath取出iframe里的元素

Javascript 使用xpath取出iframe里的元素

作者:互联网

function getAllNode(xpath, iframeId) {
        if (arguments.length == 1 || (arguments.length > 1 && (!iframeId || iframeId == "null" || iframeId == "undefined" || iframeId == "none"))) 
        {
            varxresult = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
        } else if (arguments.length > 1) 
        {
            var xresult = document.getElementById(iframeId).contentWindow.document.evaluate(xpath, document.getElementById(iframeId).contentWindow.document, null, XPathResult.ANY_TYPE, null);
        }
        var xnodes = [];
        var xres;
        while (xres = xresult.iterateNext()) {
            xnodes.push(xres);
        }
        return xnodes;
}

调用:iframeId为test

let res = getAllNode('/html/body/div[1]/div/div[4]/div[2]/div/ul/li[3]', 'test')
console.log(res[0].innerText)

标签:xpath,document,iframeId,Javascript,arguments,iframe,div,null
来源: https://www.cnblogs.com/foreversag/p/16330203.html