0

I want to do the following:

  1. Go to a website
  2. Click on a button => a javascript function will change parts of the html
  3. Fetch some contents of the newly changed html

All these automatically

I tried several tools provided for nodejs including node-cralwer and PhantomJS

Currently, I have this code running in PhantomJS

page.open('https://www.thegioididong.com/dtdd/iphone-x-256gb', function (status) {
    console.log("Status: " + status);

    var a = page.evaluate(function () {

        document.getElementsByClassName("viewparameterfull")[0].click()

        console.log(document.getElementsByClassName('parameterfull')[0].textContent)
        return document.getElementsByClassName('parameterfull')[0].textContent
    })
    phantom.exit();
})

and the output was

Status: success
ReferenceError: Can't find variable: getFullSpec

Using Chrome Debugger, I can see that getFullSpec is a function that:

  • Is fired when the button selected in the code is clicked

  • Can be found on another js file that is downloaded when the page loads.

My questions are:

  • When PhantomJS opens a link with page.open(), does it load every file (js, css...) like the browser does?

  • If it does, how can I use PhantomJS to run that getFullSpec (which is contained in another js file)?

Tran Triet
  • 1,257
  • 2
  • 16
  • 34

1 Answers1

1
  1. Yes.
  2. Js in eveluate method will be executed in page, and there is an error in onclick listener.

You can open a page, open devtools, search for code in js sources.

Vayrex
  • 1,417
  • 1
  • 11
  • 13
  • thanks for fast response! So you mean I can use PhantomJS to do this but I have error in my code? Can you tell me how to fix it? – Tran Triet Mar 25 '18 at 19:30
  • It is error in page js file. I don't know how to fix, maybe getFullSpec is not initialized yet. So try to set delay before clicking. – Vayrex Mar 25 '18 at 19:34
  • Sorry I don't know if I get you right. If you mean the source's javascript has an error, I think that's not the case cause it works when I loaded it in Chrome. – Tran Triet Mar 25 '18 at 19:38
  • 1
    Then you are doing a click before js.file which contains getFullSpec() is loaded. I don't know full phantom api, but try to eveluate you script, when all page js are loaded. Here is similar issue https://stackoverflow.com/questions/11340038/phantomjs-not-waiting-for-full-page-load. – Vayrex Mar 25 '18 at 19:42
  • I have found the solution after many trials and errors with asynchronous functions. The problem seems to be what you said - I click before the javascript file was loaded. If you could, please write your answer in a separate post so I can mark it as the answer! – Tran Triet Mar 25 '18 at 21:42
  • 1
    We came to proper solution in this post, so seems it must be the answer. I can edit it, but seems it is not needed. – Vayrex Mar 25 '18 at 21:47