javascript - Evaluating xpath expression in Chrome - Stack Overflow

I'm trying to extract a few rows from the table from this page using xpath expression and javasc

I'm trying to extract a few rows from the table from this page / using xpath expression and javascript. I can get the whole page being displayed as a pop-up but I cannot evalute xpath expression with document.evaluate(); Have tried to play with XPathResultType but no results. Can anybody help?

Here is my background page:

<html><head><script>
...
var wholePage;
setInterval(fetch, 20000);


    function fetch()
    {
        req = new XMLHttpRequest();
        var url = "/";
        req.open("GET", url);
        req.onload = process;
        req.send();
    }   

    function process()
    {
        wholePage = req.responseText;
    }
</script></head></html>

and here is the popup page:

<html><head><script>
...
    onload = setTimeout(extract, 0); 

        function extract()  
        {
            chrome.browserAction.setBadgeText({text: ''});
            var bg = chrome.extension.getBackgroundPage();
            var EurPlnPath = "//tr[@id='tabr_eurpln']";
            var tempDiv = document.getElementById('current');
            tempDiv.innerHTML = bg.wholePage;
            var oneTopic = document.evaluate( EurPlnPath, bg.wholePage, null, XPathResult.ANY_TYPE, null)
            var res = oneTopic.iterateNext();
        }

</script></head>
<body>
<div id="current">
</div>
</body>
</html>

I'm trying to extract a few rows from the table from this page http://www.money.pl/pieniadze/ using xpath expression and javascript. I can get the whole page being displayed as a pop-up but I cannot evalute xpath expression with document.evaluate(); Have tried to play with XPathResultType but no results. Can anybody help?

Here is my background page:

<html><head><script>
...
var wholePage;
setInterval(fetch, 20000);


    function fetch()
    {
        req = new XMLHttpRequest();
        var url = "http://www.money.pl/pieniadze/";
        req.open("GET", url);
        req.onload = process;
        req.send();
    }   

    function process()
    {
        wholePage = req.responseText;
    }
</script></head></html>

and here is the popup page:

<html><head><script>
...
    onload = setTimeout(extract, 0); 

        function extract()  
        {
            chrome.browserAction.setBadgeText({text: ''});
            var bg = chrome.extension.getBackgroundPage();
            var EurPlnPath = "//tr[@id='tabr_eurpln']";
            var tempDiv = document.getElementById('current');
            tempDiv.innerHTML = bg.wholePage;
            var oneTopic = document.evaluate( EurPlnPath, bg.wholePage, null, XPathResult.ANY_TYPE, null)
            var res = oneTopic.iterateNext();
        }

</script></head>
<body>
<div id="current">
</div>
</body>
</html>
Share Improve this question edited Sep 9, 2015 at 13:19 sideshowbarker 88.6k30 gold badges215 silver badges212 bronze badges asked Feb 25, 2012 at 18:23 matcheekmatcheek 5,15710 gold badges50 silver badges77 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can't use XPath on plain strings. You have to convert the string into a document first. For example, using the DOMParser. Current browsers do not support text/html yet. To get this to work, you have to include the code as specified at this answer:

var bgWholePage = new DOMParser().parseFromString(bg.wholePage, 'text/html');
document.evaluate( EurPlnPath, bgWholePage, ...

If you want to parse the document at the background page, use bg.document.evaluate instead of document.evaluate:

var oneTopic = bg.document.evaluate( EurPlnPath, bg.wholePage, null, XPathResult.ANY_TYPE, null)

Try document.querySelector("tr#tabr_eurpln") instead of document.evaluate, this will return a DOM element, corresponding to selector.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745294941a4621082.html

相关推荐

  • javascript - Evaluating xpath expression in Chrome - Stack Overflow

    I'm trying to extract a few rows from the table from this page using xpath expression and javasc

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信