Reading javascript variable from website using Pha

2019-01-15 19:44发布

I'm new to Javascript and PhantomJS, but my seemly simple objective has proven to be harder to achieve than expected. I want to write a script that would load a website, and then output the value of a Javascript variable used on that page. If I open that page in a browser and open the Javascript console, I can type in the variable name and it tells me the value associated to said variable. I just trying to reproduce this function but with PhantomJS so that I can automate this task.

Could someone point me towards the right documentation for this? I haven't been able to find how to do such a thing, assuming PhantomJS is the right way to do this. Maybe there is a simpler alternative?

Thanks.

2条回答
淡お忘
2楼-- · 2019-01-15 20:04

What you need to understand is that phantomJS has two JavaScript environments and those two are independent of each other. The inner one is the document script (which you have in any browser). The outer one is controlling what phantomJS should do. It simulates the user.

So in a sense you need to tell phantomJS "the user opened the JavaScript console any typed ...". The evaluate command does this.

So to read the value of the variable foo, you write this code:

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

Note: The document isn't strictly necessary but it helps to keep the two environments apart in the head of the developer.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-15 20:09

Aaron's answer didn't work for me. Maybe it was valid back in 2013, but now (2016), it doesn't work anymore with the current version. In Aaron's example, foo returns a promise, not a value. console.log(foo) outputs Promise { <pending> }.

Here's what I figured out :

page
    .evaluate( function(){ return window.foo })
    .then ( function(foo){ console.log "foo = ", foo});

Now you really get the value of foo.

查看更多
登录 后发表回答