评估 Chrome 中的 xpath 表达式
我正在尝试从此页面的表中提取几行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在纯字符串上使用 XPath。您必须首先将字符串转换为文档。例如,使用
DOMParser
。当前的浏览器尚不支持text/html
。要使其正常工作,您必须在此包含 指定的代码回答:如果要在后台页面解析文档,请使用
bg.document.evaluate
而不是document.evaluate
: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 supporttext/html
yet. To get this to work, you have to include the code as specified at this answer:If you want to parse the document at the background page, use
bg.document.evaluate
instead ofdocument.evaluate
:尝试使用
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.