admin管理员组文章数量:1418699
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
2 Answers
Reset to default 3You 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.
本文标签: javascriptEvaluating xpath expression in ChromeStack Overflow
版权声明:本文标题:javascript - Evaluating xpath expression in Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745294940a2652018.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论