How to return value from an extended casperjs function?

fat fantasma

I am trying to return a value from the below function like so.

html = casper.get_HTML(myselector);

All I am getting returned is "undefined" (return_html). However, the 'html' variable is getting set properly. The over all function works properly. It's just the return value that is the issue.

How do you do it?

casper.get_HTML = function(myselector) {
    var return_html;

    casper.waitForSelector(myselector,
        function() {
            var html = casper.getHTML(myselector, false);
            return_html = html;                                     //got the html
        },
        function() {                                                // Do this on timeout
            return_html = null;
        },
       10000                                                       // wait 10 secs
    );

    return return_html;
 };
Artjom B.

In CasperJS all then* and all wait* functions are step functions which are asynchronous. It means that you cannot return something that is determined asynchronously in your custom function. You have to use a callback:

casper.get_HTML = function(myselector, callback) {
    this.waitForSelector(myselector,
        function then() {
            var html = this.getHTML(myselector, false);
            callback(html);
        },
        function onTimeout() {
            callback();
        },
        10000 // wait 10 secs
    );
    return this; // return this so that you can chain the calls
};

casper.start(url).get_HTML("#myid", function(html){
    if (html) {
        this.echo("success");
    } else {
        this.echo("failed");
    }
}).run();

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to return value from debounced function in javascript?

分類Dev

How to correctly return value from function?

分類Dev

How to return value from a child process in a function?

分類Dev

How to get a return value from a connect function

分類Dev

How to return one and only one value from a PowerShell function?

分類Dev

How to get the single return value from mysql stored function in php

分類Dev

How can I return a value from a parent function in Node?

分類Dev

PHP: how to return counter of value from recursive function?

分類Dev

How to properly return value from a data inside the function

分類Dev

How to return a value from a async function in array.map in javascript?

分類Dev

Lua: How to assigned a return value from a function to a local value of a different function in lua

分類Dev

How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

分類Dev

How to return a list from a function?

分類Dev

How to return value from a Promise

分類Dev

Return value from a function callback in a for loop

分類Dev

Cannot return value from a function in other file

分類Dev

getting address of pointer from function return value

分類Dev

How to capture function return value with if condition

分類Dev

How to unwrap return value at function call?

分類Dev

How to process return value of main() function?

分類Dev

Python: How to obtain return value from only one function (out of several executed with asyncio.gather)

分類Dev

How to call python script from CasperJS

分類Dev

Return value from setInterval() function called from html

分類Dev

How to get the return values from a function?

分類Dev

React native how to return a string from a function

分類Dev

how to return observable from function that has a promise

分類Dev

How to return result in series from R function?

分類Dev

How to return value from JavaScript using Selenium?

分類Dev

Get return value from c# function and display in html

Related 関連記事

  1. 1

    How to return value from debounced function in javascript?

  2. 2

    How to correctly return value from function?

  3. 3

    How to return value from a child process in a function?

  4. 4

    How to get a return value from a connect function

  5. 5

    How to return one and only one value from a PowerShell function?

  6. 6

    How to get the single return value from mysql stored function in php

  7. 7

    How can I return a value from a parent function in Node?

  8. 8

    PHP: how to return counter of value from recursive function?

  9. 9

    How to properly return value from a data inside the function

  10. 10

    How to return a value from a async function in array.map in javascript?

  11. 11

    Lua: How to assigned a return value from a function to a local value of a different function in lua

  12. 12

    How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

  13. 13

    How to return a list from a function?

  14. 14

    How to return value from a Promise

  15. 15

    Return value from a function callback in a for loop

  16. 16

    Cannot return value from a function in other file

  17. 17

    getting address of pointer from function return value

  18. 18

    How to capture function return value with if condition

  19. 19

    How to unwrap return value at function call?

  20. 20

    How to process return value of main() function?

  21. 21

    Python: How to obtain return value from only one function (out of several executed with asyncio.gather)

  22. 22

    How to call python script from CasperJS

  23. 23

    Return value from setInterval() function called from html

  24. 24

    How to get the return values from a function?

  25. 25

    React native how to return a string from a function

  26. 26

    how to return observable from function that has a promise

  27. 27

    How to return result in series from R function?

  28. 28

    How to return value from JavaScript using Selenium?

  29. 29

    Get return value from c# function and display in html

ホットタグ

アーカイブ