'How Can I log-in Amazon with Golang Colly

I am trying login to my amazon buyer account for getting tracking info. I made wordpress-woocommerce login and getting infos but I could not for Amazon.

package main

import (
    "fmt"
    "log"

    "github.com/gocolly/colly"
)

func main() {
    // create a new collector
    c := colly.NewCollector()
    login_link := "https://www.amazon.de/-/en/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.de%2F%3Flanguage%3Den_GB%26ref_%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=deflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&"
    // authenticate
    err := c.Post(login_link, map[string]string{"username": "[email protected]", "password": "123qwerty"})
    if err != nil {
        log.Fatal(err)
    }

    // attach callbacks after login
    c.OnResponse(func(r *colly.Response) {
        log.Println("response received", r.StatusCode) //response received 200
    })

    c.OnHTML("div", func(h *colly.HTMLElement) {
        fmt.Println("PRINT ALL: ", h.Text)
    })

    // start scraping
    c.Visit("https://www.amazon.de/-/en/gp/your-account/order-history?ref_=ya_d_c_yo")
}

Wordpress Login One Page - Amazon Login Two Page. We probably need to scroll 2 pages for Amazon https://i.stack.imgur.com/4TNj5.png -> Wordpress Login (One Page)
https://i.stack.imgur.com/bhE4m.png -> Amazon Login(Page #1 - Mail)
https://i.stack.imgur.com/0BFcA.png -> Amazon Login(Page #1 - Password)



Solution 1:[1]

chromedp is a very useful library in such cases. You may try the following snipet;

package main

import (
    "context"
    
    "os"
    "time"

    "github.com/chromedp/chromedp"
)

func main() {
    
    var res []byte
    ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithBrowserOption())
    defer cancel()
    err := chromedp.Run(ctx,
        chromedp.Navigate("https://www.amazon.com"),
        chromedp.WaitReady("body"),
        chromedp.Click(`a[data-nav-role="signin"]`, chromedp.ByQuery),
        chromedp.Sleep(time.Second*2),
        chromedp.SetValue(`ap_email`, "youramazonemail", chromedp.ByID),
        chromedp.Click(`continue`, chromedp.ByID),
        chromedp.Sleep(time.Second*1),
        chromedp.SetValue(`ap_password`, "youramazonpassword", chromedp.ByID),
        chromedp.Click(`signInSubmit`, chromedp.ByID),
        chromedp.Sleep(time.Second*2),
        chromedp.CaptureScreenshot(&res),
    )
    if err != nil {
        log.Fatal(err)
    }
    os.WriteFile("loggedin.png", res, 0644)
}

The example given above is basically navigates trough all steps required for login process. After successful login, you can use context (ctx) to navigate and get the information whatever you want by using same function.

chromedp.Run(ctx,
        chromedp.Navigate(url),
        ...)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1