我正在尝试在带有Golang和sclevine/agouti的AWS Lambda上使用无头铬。
我确认使用exec.Command
在Lambda上执行了chromedriver。
拍摄屏幕快照时发生错误,但是我不确定无头铬是否已正确启动。执行driver.Start()
和driver.NewPage
时未发生错误。未显示诸如ChromeDriver 2.43.600233, onlt local connection is allowed
之类的开始消息。
代码和输出如下。
func main() {
lambda.Start(handler)
}
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
os.Setenv("PATH", os.Getenv("PATH") + ":/var/task")
out1, err := exec.Command("/var/task/chromedriver", "-v").Output()
if err != nil {
log.Fatal(err)
}
log.Printf("exec chromedriver %s\n", string(out1))
// 2018/11/08 13:12:05 exec chromedriver ChromeDriver 2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d)
opts := []agouti.Option{
agouti.ChromeOptions(
"args", []string{
"headless",
"no-default-browser-check",
"verbose",
"no-sandbox",
"no-first-run",
"disable-default-apps",
"disable-popup-blocking",
"disable-translate",
"disable-background-timer-throttling",
"disable-renderer-backgrounding",
"disable-device-discovery-notifications",
},
),
agouti.Desired(
agouti.Capabilities{
"loggingPrefs": map[string]string{
"browser": "INFO",
},
},
),
}
opts = append(opts,
agouti.ChromeOptions(
"binary", "/var/task/headless-chromium",
))
driver := agouti.NewWebDriver("http://{{.Address}}", []string{"/var/task/chromedriver", "--port={{.Port}}"}, opts...)
if err := driver.Start(); err != nil {
log.Fatalf("failed to start driver:%v", err)
}
p, err := driver.NewPage()
if err != nil {
log.Fatalf("failed to open page:%v", err)
}
if err := page.Navigate("https://www.google.com/"); err != nil {
log.Fatalf("failed to navigate:%v", err)
}
if err := page.Screenshot("/tmp/top.jpg"); err != nil {
log.Fatalf("failed to take ss:%v", err)
// 2018/11/08 13:12:07 failed to take ss:failed to retrieve
// screenshot: unexpected response:
// {
// "sessionId": "30c4fc0e74ce6c865ced6c8443e873bc",
// "status": 6,
// "value": {
// "message": "invalid session id\n (Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.14.72-68.55.amzn1.x86_64 x86_64)"
// }
//}
}
}
我得到了headless-shell和chromedriver的二进制文件,如下所示。
curl -SL https://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip > chromedriver.zip
unzip chromedriver.zip -d artifact/
rm chromedriver.zip
curl -SL https://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-54/stable-headless-chromium-amazonlinux-2017-03.zip > headless-chromium.zip
unzip headless-chromium.zip -d artifact/
rm headless-chromium.zip
我使用SAM部署了工件,工件被部署在Lambda上的var/tasks/
下。
请帮助我...