无法弄清楚为什么我无法使用Xpath检索一个简单的字符串 [英] Can't figure out why I can't retrieve a simple string with Xpath

查看:196
本文介绍了无法弄清楚为什么我无法使用Xpath检索一个简单的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么我无法用这个非常简单的代码片段检索带有XPath的简单字符串

I can't figure out why I can't retrieve a simple string with XPath with this very simple snippet

var page = new WebPage();
page.open('http://free.fr', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        function getElementByXpath(path) {
          return document.evaluate(path, document, null, XPathResult.STRING_TYPE, null).stringValue;
        }

        console.log( getElementByXpath("//title/text()") );
    }
    phantom.exit();
}

总是不返回任何内容。

我错过了打印的内容标题值?

What I missed to print the title value?

推荐答案

PhantomJS有两个上下文。只有DOM上下文(页面上下文)可以访问DOM,但它是沙箱。您可以通过 page.evaluate 。但请记住:

PhantomJS has two contexts. Only the DOM context (page context) has access to the DOM, but it is sandboxed. You get access to the DOM context through page.evaluate. But remember that:


注意:参数和返回值到 evaluate 函数必须是一个简单的原始对象。经验法则:如果它可以通过JSON序列化,那就没问题了。

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

闭包,函数,DOM节点等将 not 工作!

Closures, functions, DOM nodes, etc. will not work!

这意味着您无法将找到的任何DOM节点传递给外部上下文。虽然,在DOM上下文之外有一个文档对象,但它没有做任何事情。它只是PhantomJS在QtWebkit上编写的一种方式。

This means that you cannot pass any DOM node that you find to the outer context. Although, there is a document object outside of the DOM context, but it doesn't do anything. It's only a relict of the way PhantomJS is written on top of QtWebkit.

以下是一个示例修复:

var page = new WebPage();
page.onConsoleMessage = function(msg){
    console.log("remote: " + msg);
};
page.open('http://google.fr', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        page.evaluate(function(){
            function getElementByXpath(path) {
              return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
            }

            console.log( getElementByXpath("//head/title/text()").textContent );
        });
    }
    phantom.exit();
});

这篇关于无法弄清楚为什么我无法使用Xpath检索一个简单的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆