benbjohnson / phantomjs Goto Github PK
View Code? Open in Web Editor NEWGo client for PhantomJS.
License: MIT License
Go client for PhantomJS.
License: MIT License
The only code is here.
package main
import (
"github.com/benbjohnson/phantomjs"
)
func main() {
// Start the process once.
if err := phantomjs.DefaultProcess.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}
defer phantomjs.DefaultProcess.Close()
}
It reports
Fatal Windows exception, code 0xc0000005.
PhantomJS has crashed. Please read the bug reporting guide at
<http://phantomjs.org/bug-reporting.html> and file a bug report.
But on macOS, it works well.
Maybe this is related with shim.js
?
When I make requests over SSL I get an error, I've tried using page.SetSettings but I can't find a setting that ignores SSL errors or something along those lines.
// Works fine, loads the page, etc.
if err := page.Open("http://whatsmyuseragent.org"); err != nil {
panic(err)
}
// err isn't nil
if err := page.Open("https://google.com"); err != nil {
panic(err)
}
Stacktrace:
panic: failed
goroutine 1 [running]:
panic(0x624820, 0xc4200e0570)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
/home/gianluca/Desktop/go-test/main.go:42 +0x554
I don't have any open source contribution experience,
don't know how to create patch file.
Sorry for inconvenience
Here is fix
func (p *Process) Open() error {
(...)
// Follwing line cause crash.
// cmd.Env = []string{fmt.Sprintf("PORT=%d", p.Port)} // original code
// After slight modification into next line into this one, there is no more crash.
cmd.Env = append(os.Environ(), fmt.Sprintf("PORT=%d", p.Port)) // fixed code
(...)
}
Thank you.
.
I use something like this:
"use strict"
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000,
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx());
} else {
if(!condition) {
phantom.exit(1);
} else {
typeof(onReady) === "string" ? eval(onReady) : onReady();
clearInterval(interval);
}
}
}, 250);
};
var page = require('webpage').create();
page.open('https://www.somewebsite.com', function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
waitFor(function() {
return page.evaluate(function() {
var bets = document.getElementsByClassName('someclass').length;
if (bets < 1) {
return false;
}
return true;
});
}, function() {
console.log(page.evaluate(function() {
return document.documentElement.innerHTML;
}))
}, 20000);
}
});
What is the better way to do something like this with this package?
Thanks for your help.
How to use proxy? thanks
Is it possible to return a group of css selectors from this function then pass it back into another Evaluate function later on ? Or an equivalent workaround ? Thank you.
What are the differences between Evaluate() and EvaluateJavaScript() aside from the name of the functions and the comment descriptions, they appear to be the same thing. What am I missing ? Thank you Ben.
// EvaluateJavaScript executes a JavaScript function.
// Returns the value returned by the function.
func (p *WebPage) EvaluateJavaScript(script string) (interface{}, error) {
var resp struct {
ReturnValue interface{} `json:"returnValue"`
}
if err := p.ref.process.doJSON("POST", "/webpage/EvaluateJavaScript", map[string]interface{}{"ref": p.ref.id, "script": script}, &resp); err != nil {
return nil, err
}
return resp.ReturnValue, nil
}
// Evaluate executes a JavaScript function in the context of the web page.
// Returns the value returned by the function.
func (p *WebPage) Evaluate(script string) (interface{}, error) {
var resp struct {
ReturnValue interface{} `json:"returnValue"`
}
if err := p.ref.process.doJSON("POST", "/webpage/Evaluate", map[string]interface{}{"ref": p.ref.id, "script": script}, &resp); err != nil {
return nil, err
}
return resp.ReturnValue, nil
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.