首页 \ 问答 \ 使用CasperJS中的函数返回iframe内的链接(Returning links inside iframe using a function in CasperJS)

使用CasperJS中的函数返回iframe内的链接(Returning links inside iframe using a function in CasperJS)

我试图从iframe内部获取链接并将它们作为函数结果返回,我的简化代码看起来像这样:

var casper = require("casper").create({
    verbose: true,
    logLevel: "debug",
        webSecurityEnabled: false
});

var url = casper.cli.get(0);

casper.on('remote.message', function(msg) {
    this.echo(msg);
})

casper.start(url, function () {
    thelinks = getLinksFromIframes( casper );
    console.log("doesn't work:" + thelinks);
});

function getLinksFromIframes( context ) {
        var links = [];

        var iframes = context.evaluate( function() {
                var iframes = [];
                [].forEach.call(document.querySelectorAll("iframe"), function(iframe, i) { iframes.push( i ); });
                return iframes;
        });

        iframes.forEach( function( index ) {
            context.withFrame(index, function() {
                links = links.concat( this.getElementsAttribute( 'a', 'href' ) );
                console.log("works: " + links);
            });
        });

        return links;
}

casper.run(function() {
    console.log('done');
    this.exit();
});

问题是该函数没有返回任何内容,我只能读取withFrame里面的链接var,我知道还有其他方法来获取链接,但代码是这样的,因为它是一些更复杂的部分,将分析嵌套iframe以及iframe内iframe的数量未知。 有没有什么方法可以等待withFrame或允许我返回链接作为函数结果的东西?


I am trying to get the links from inside an iframe and return them as a function result, my simplified code looks something like this:

var casper = require("casper").create({
    verbose: true,
    logLevel: "debug",
        webSecurityEnabled: false
});

var url = casper.cli.get(0);

casper.on('remote.message', function(msg) {
    this.echo(msg);
})

casper.start(url, function () {
    thelinks = getLinksFromIframes( casper );
    console.log("doesn't work:" + thelinks);
});

function getLinksFromIframes( context ) {
        var links = [];

        var iframes = context.evaluate( function() {
                var iframes = [];
                [].forEach.call(document.querySelectorAll("iframe"), function(iframe, i) { iframes.push( i ); });
                return iframes;
        });

        iframes.forEach( function( index ) {
            context.withFrame(index, function() {
                links = links.concat( this.getElementsAttribute( 'a', 'href' ) );
                console.log("works: " + links);
            });
        });

        return links;
}

casper.run(function() {
    console.log('done');
    this.exit();
});

The problem is that the function doesn't return anything, I can only read the links var inside withFrame, i know there are other ways to get the links, but the code is this way because it part of something more complex that will analyze nested iframes, and the amount of iframes inside iframes is unknown. Is there any way I could wait on withFrame or something that will allow me to return the links as the function result?


原文:https://stackoverflow.com/questions/35672411
更新时间:2024-03-30 20:03

最满意答案

如果你真的必须这样做,你将需要使用括号:

defmodule Module2 do
  def func([c], n), do: (IO.inspect(c); c + n)
end

原文的问题在于; vs函数/宏调用,因为它是这样解析的:

defmodule Module2 do
  (def func([c], n), do: IO.inspect(c)); c + n
end

你可以验证这给出了你提到的完全相同的错误 - 编译器自然会抱怨,因为你试图在函数的上下文之外使用c


If you really must do this, you will need to use parentheses:

defmodule Module2 do
  def func([c], n), do: (IO.inspect(c); c + n)
end

The problem with the original is the precedence of ; vs function/macro calls, because of which it is parsed like this:

defmodule Module2 do
  (def func([c], n), do: IO.inspect(c)); c + n
end

You can verify that this gives the exact same error you mention - the compiler naturally complains because you're trying to use c outside of the context of the function.

相关问答

更多

相关文章

更多

最新问答

更多
  • linux环境下怎么用maven编译一个已有的java项目?
  • Ionic2和IonicDeploy(Ionic2 and IonicDeploy)
  • 尝试分组和别名数据时出错(Error when Attempting to Group and Alias Data)
  • Facebook PHP API错误代码100:API中提供的appsecret_proof无效(Facebook PHP API Error code 100: Invalid appsecret_proof provided in the API)
  • 不用u盘和光盘怎么安装linux系统
  • 表名作为变量参数(Table name as variable parameter)
  • 我想学计算机 在望京花园住 附近那有这样的地方可以学
  • 删除原始记录的重复项(Removing duplicates with original records)
  • Pandas:使用.astype(str)时忽略空值?(Pandas: ignore null values when using .astype(str)?)
  • Python新手阵列 - 列表转换[重复](Python newbie array - list conversion [duplicate])
  • java.lang.String cannot be cast to java.lang.Float
  • 带参数的选择器(Selector with argument)
  • python中的for循环语法(for loop syntax in python)
  • Nunit - 无法检索类别属性(NUnit - Can't retrieve a category property)
  • python3 xpath无法到达子节点(AttributeError:'NoneType'对象没有属性'text')(python3 xpath can't reach a child node (AttributeError: 'NoneType' object has no attribute 'text'))
  • Android自动 - 如何启用开发人员模式(Android auto - how to enable developer mode)
  • java weka stringtowordvector没有正确计算单词出现次数(java weka stringtowordvector is not counting word occurences properly)
  • 如何解释DHT响应中的“节点”?(How to interpret 'nodes' in a DHT response?)
  • 什么是使用opencv和树莓相机模块进行人脸检测的最佳算法(What is the best algorithm for face detection using opencv and raspberry camera module)
  • 可以somone解释这两个问题的解决方案(c程序,mutex,线程)吗?(Can someone explain the solution of these two questions (c program,mutex,threads)?)
  • 如何解决SyntaxError:意想不到的令牌var gulp(How to fix SyntaxError: Unexpected token var in gulp)
  • 在满足R中的第一个实例后,在所有行中分配值(Assign value in all rows following satisfaction of first instance in R)
  • 如何记录Atk4 auth /登录事件(How to log Atk4 auth/login event)
  • 是否有必要在新的scala项目中添加我的自定义scala库依赖项?(is it necessary to add my custom scala library dependencies in new scala project?)
  • Java中对象类的继承(Inheritance of Object Class in Java)
  • 如何在jquery自动完成中获取当前元素?(How to get current element in jquery autocomplete?)
  • 学电子商务甚么大学好
  • @Override在java中(@Override in java)
  • 虚拟机中怎么给linux装office
  • Laravel邮件参数(sendmail / smtp)(Laravel mail parameters (sendmail/smtp))