Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

phantomjs page.open's callback recalled #10353

Closed
ariya opened this issue Jan 14, 2012 · 6 comments
Closed

phantomjs page.open's callback recalled #10353

ariya opened this issue Jan 14, 2012 · 6 comments
Labels

Comments

@ariya
Copy link
Owner

ariya commented Jan 14, 2012

powerp...@gmail.com commented:

Which version of PhantomJS are you using? Tip: run 'phantomjs --version'.
phantomjs 1.4.1

What steps will reproduce the problem?
1../phantomjs multi_sniff.js www.baidu.com www.qq.com www.sina.com www.xunlei.com www.sohu.com www.taobao.com www.dangdang.com www.mtime.com www.autohome.com.cn www.tianyabook.com

What is the expected output? What do you see instead?
Would see log time string of all input urls.
In some test round, I found such log:
[www.tianyabook.com] Loading time 13072 msec
[www.sohu.com] Loading time 15072 msec
[www.autohome.com.cn] Loading time 15207 msec
[www.mtime.com] Loading time 15262 msec
[www.qq.com] Loading time 15591 msec
[www.taobao.com] Loading time 15640 msec
www.tianyabook.com recalled
www.tianyabook.com recalled
[www.sina.com] Loading time 17149 msec

page.open's callback function has recalled twice after 'success', that means called 3 times totally.

Which operating system are you using?
OpenSuSE 11.4 x64 Desktop and Windows
Occurs on opensuse more frequently than windows.

Did you use binary PhantomJS or did you compile it from source?
binary.

Please provide any additional information below.
multi_sniff.js intends to fetch multi pages concurrently, and record har for each url. I made this by copying and modifying codes from examples.

Occasionally, I found some url generated har more than once, so I add a code snippet in if..else of page.open's callback, to avoid reenter:

function fetchPage(url) {
...
var called = 0;
page.open(..., function(status)
{
if (status !== 'success') {...}
else {
if (called) {
console.log(page.address + ' recalled');
return;
}

 called = 1;
 ...

});
}

Disclaimer:
This issue was migrated on 2013-03-15 from the project's former issue tracker on Google Code, Issue #353.
🌟   8 people had starred this issue at the time of migration.

@ariya
Copy link
Owner Author

ariya commented Jan 17, 2012

powerp...@gmail.com commented:

I find the reason: there should be some redirection, by meta refresh/js window.location, each new visit will raise the page.open callback again.
So I modified the callback function as following snippet:

            if (called) {
                console.log('[' + page.address + '] recalled');
                return;
            }

            called= 1;

            page.title = page.evaluate(function () {
                return document.title;
            });

            console.log('[' + page.address + '] status: ' + status);

            if (page.activity === 0) { // activity is updated in page action event
                console.log('[' + page.address + '] loading time1(browser) failed');
            }
            else {
                console.log('[' + page.address + '] loading time1(browser) ' + (page.activity - page.startTimeMSec) + ' msec');
            }

            waitFor2(function () {
                // wait more 1.5s to ensure no more page action event 
                return new Date().getTime() - page.activity > 1500;
            }, function () {
                if (page.activity === 0) {
                    console.log('[' + page.address + '] loading time2(network) failed ');
                }
                else {
                    console.log('[' + page.address + '] loading time2(network) ' + (page.activity - page.startTimeMSec) + ' msec');
                }

                outputPage(page);

                page.release();

                catch (e) {
                    console.log(e);
                }
            }, function () {
                console.log('[' + page.address + '] timeouts.');
                outputPage(page);

                page.release();
            }, timeout);

waitFor2 is copied from waitFor in example, only difference is clearInterval instead of phantom.exit.

No matter, 'success' or 'failed', script will wait until no more page activity.
It works well for me now.

@emezeske
Copy link

emeze...@gmail.com commented:

I have also run into this problem. I have a simple script that calls page.open() and then does nothing. The page in question is doing some XMLHTTPRequests in the background, and PhantomJS really just needs to let it do its thing.

The function that I pass to page.open(), though, gets called twice, both times being passed "success", and then a third time, being passed "fail".

Strangely, everything works properly when I introduce a "called" boolean and return from the page.open() callback immediately if it has already been called. Here's the PhantomJS script where this occurs:

https://github.com/emezeske/lein-cljsbuild/blob/0.1.3/example-projects/advanced/phantom/repl.js

@JamesMGreene
Copy link
Collaborator

james.m....@gmail.com commented:

I have also seen this on pages that use frames/iframes, such as the JsUnit test runner and QUnit "composite" (addon) test runner. It may or may not also happen when child windows are opened (i.e. window.open invocations), not sure.

@JamesMGreene
Copy link
Collaborator

james.m....@gmail.com commented:

Cross reference issue for frames/iframes: http://code.google.com/p/phantomjs/issues/detail?id=504

@varunshaji
Copy link

Can anyone throw light on how they resolved this issue. I am having problem with multiple page load occurring due to iframe in the requested page.Is the solution mentioned by Ariya the final one ?
@ariya Could you give a clearer mention of the solution. I couldn't understand many thinks like page.activity,clearInterval etc .
could you expand the solution and give clarity to it. It would be much helpful to others like me looking here
Thanks

@ghost ghost removed old.Priority-Medium labels Dec 19, 2017
@stale stale bot added the stale label Dec 26, 2019
@stale
Copy link

stale bot commented Dec 29, 2019

Due to our very limited maintenance capacity (see #14541 for more details), we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed. In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution!

@stale stale bot closed this as completed Dec 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants