0

I'm trying to log into Youtube, which redirects to accounts.google.com/ServiceLogin?service=youtube... with chromedp. For some reason it times out trying to retrieve anything from that page. Most of the selectors have randomly generated classes but that shouldn't prevent fetching body, which is does. Screenshot shows the login page so it's navigating there correctly but it can't find anything on the page. There are iframes but the login form/input isn't in one, and the #identifierId can't be found.

Here is a code snippet:

func main() {
    // create chrome instance
    var buf []byte
    ctx, cancel := chromedp.NewContext(
        context.Background(),
        // chromedp.WithDebugf(log.Printf),
    )
    defer cancel()

    // create a timeout
    ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
    defer cancel()

    // navigate to a page, wait for an element, click
    var u string
    err := chromedp.Run(ctx,
        chromedp.Navigate(`https://youtube.com`),
        chromedp.Click(`#buttons > ytd-button-renderer > a`),
        chromedp.SendKeys(`#identifierId`, "bobboshmo@gmail.com", chromedp.ByID),
        chromedp.Location(&u),
    )
    if err != nil {
        log.Fatal(err)
    }
    log.Printf(u)

    // capture entire browser viewport, returning png with quality=90
    if err := chromedp.Run(ctx, fullScreenshot(u, 90, &buf)); err != nil {
        log.Fatal(err)
    }
    if err := ioutil.WriteFile("fullScreenshot.png", buf, 0o644); err != nil {
        log.Fatal(err)
    }

}

func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {
    return chromedp.Tasks{
        chromedp.Navigate(urlstr),
        chromedp.FullScreenshot(res, quality),
    }
}
user229044
  • 232,980
  • 40
  • 330
  • 338
vinniyo
  • 145
  • 14

1 Answers1

1

Turns out Google accounts page was responding differently based on the User-Agent so When I'm browsing and grabbing the xpath or selector they were different than what my go program was using. Fixed by adding the same useragent into Run at the start:

emulation.SetUserAgentOverride(`Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Mobile Safari/537.36`),
vinniyo
  • 145
  • 14