评估 Chrome 中的 xpath 表达式

发布于 2025-01-08 19:37:29 字数 1402 浏览 0 评论 0原文

我正在尝试从此页面的表中提取几行 http://www.money.pl/pieniadze / 使用 xpath 表达式和 javascript。 我可以将整个页面显示为弹出窗口,但无法使用 document.evaluate(); 评估 xpath 表达式。 尝试使用 XPathResultType 但没有结果。 有人可以帮忙吗?

这是我的背景页面:

<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>

这是弹出页面:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2025-01-15 19:37:30

您不能在纯字符串上使用 XPath。您必须首先将字符串转换为文档。例如,使用 DOMParser。当前的浏览器尚不支持 text/html。要使其正常工作,您必须在此包含 指定的代码回答

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

如果要在后台页面解析文档,请使用bg.document.evaluate而不是document.evaluate

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

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)
洛阳烟雨空心柳 2025-01-15 19:37:30

尝试使用 document.querySelector("tr#tabr_eurpln") 而不是 document.evaluate,这将返回一个 DOM 元素,对应于选择器。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文