0

Questions how to login to Amazon have been discussed for various environments on stackoverflow:

The mileage seems to vary depending on the approach taken. E.g. as of 2013 one comment was:

Amazon have since changed their login process, adding some sort of CSFR protection which makes it difficult to log in using cURL

Using Java I also had no luck trying the direct curl-like approach and fiddling with the OpenId parameters:

// https://www.amazon.com/ap/signin?
//   _encoding=UTF8&
//   openid.assoc_handle=usflex&
//   openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&
//   openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&
//   openid.mode=checkid_setup&
//   openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&
//   openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&
//   openid.pape.max_auth_age=0&
//   openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fsign-in.html%3Fie%3DUTF8%26*Version*%3D1%26*entries*%3D0

Question:

Would using a ruby mechanize - like style work here and which would be a working mechanize equivalent on Java?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186

1 Answers1

1

with Selenium see http://www.seleniumhq.org/ the login was possible in a straightforward way. See sample code below

Please note: AmazonUser is just a helper class to hold the elements needed for login:

  • email
  • password

Example usage

Amazon amazon = new Amazon();
String html = amazon
  .navigate("/gp/css/order-history/ref=oh_aui_menu_open?ie=UTF8&orderFilter=open");

Amazon helper class

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * Amazon handling
 * @author wf
 *
 */
public class Amazon {
    FirefoxDriver driver = new FirefoxDriver();
    String root="https://www.amazon.de";

    /**
     * signIn
     * @throws Exception
     */
    public void signIn() throws Exception {
        // <input type="email" autocapitalize="off" autocorrect="off" tabindex="1" maxlength="128" size="30" value="" name="email" id="ap_email" /> 
        WebElement emailField=driver.findElement(By.id("ap_email"));
        // get user and password
        AmazonUser auser=AmazonUser.getUser();
        emailField.sendKeys(auser.getEmail());
        //  <input type="password" class="password" onkeypress="displayCapsWarning(event,'ap_caps_warning', this);" tabindex="2" size="20" maxlength="1024" name="password" id="ap_password" />
        WebElement passwordField=driver.findElement(By.id("ap_password"));
        passwordField.sendKeys(auser.getPassword()); 
        // signInSubmit-input
        WebElement signinButton=driver.findElement(By.id("signInSubmit-input"));
        signinButton.click();
    }

  /**
   * navigate to the given path
   * @param path
   * @return
   * @throws Exception
   */
  public String navigate(String path) throws Exception {
    driver.get(root+path);
    String html=driver.getPageSource();
    if (html.contains("ap_signin_form")) {
      signIn();
    }
    html=driver.getPageSource();
    return html;
  }

  public void close() {
    driver.close();
    driver.quit();      
  }


}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186