Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to know is there any way to click link (open link) on PhantomJS synchronously. First page must be opened first, then click link on the first page to go to second page. Here is my approach using setTimeout:

var page = require('webpage').create();
var url = "http://domain.tld/index.html";

page.open(url, function (status) {
    page.render("1-home.png");

    page.evaluate(function() {
        // search for element and click it to redirect
        document.getElementById("RetailUser").click();
    });

    // I use setTimeout to wait for firstpage to get loaded
    setTimeout(function () {
        // do another process on second page
    }, 5000);

}

PS: I aware I can accomplish it using CasperJS. But if possible, I don't want to use CasperJS.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
565 views
Welcome To Ask or Share your Answers For Others

1 Answer

Clicking a link is always synchronously. What happens afterwards is always asynchronously, because of the asynchronous nature of JavaScript.

There are multiple ways to wait for the next page:

  • Static amount of time with setTimeout,
  • Dynamic waiting for a condition with waitFor,
  • Next page load by registering to page.onLoadFinished before the click,
  • Wait until all outstanding requests are finished (one and two) or
  • A combination of those four methods.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...