0

我正在尝试在带有 Golang 和sclevine/agouti的 AWS Lambda 上使用 headless-chromium 。我确认 chromedriver 是在 Lambda 上使用exec.Command.

截屏时出错,但我不确定 headless-chromium 是否正确启动。driver.Start()执行和时没有发生错误driver.NewPageChromeDriver 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)"
        //    }
        //}
    }
}

我得到了如下所示的无头外壳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 部署了工件,并将工件部署在var/tasks/Lambda 下。

请帮我...

4

0 回答 0