1

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": "mail@example.com", "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)

Melisa
  • 310
  • 2
  • 16
  • You're not likely to get this to work. I've tried to log in to Amazon before without a full browser and I had no luck. – pguardiario Sep 20 '21 at 00:25

1 Answers1

2

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),
        ...)
sigkilled
  • 227
  • 1
  • 7
  • 1
    thanks @signkilled . It works! But my amazon account have opt. So i extend your snippet with: chromedp.Click(`signInSubmit`, chromedp.ByID), chromedp.Sleep(time.Second*2), chromedp.SetValue(`auth-mfa-otpcode`, takeOpt(), chromedp.ByID), And for take opt: func takeOpt() string { fmt.Println("Enter your OPT: ") var opt string fmt.Scanln(&opt) fmt.Println(opt) return opt } – Elia Jan 05 '22 at 15:50