远程登录亚马逊

时间:2014-11-28 06:54:46

标签: java login amazon

针对各种环境讨论了如何登录亚马逊的问题 在stackoverflow上:

里程似乎因所采取的方法而异。例如。截至2013年,一条评论是:

亚马逊已经改变了他们的登录过程,添加了某种CSFR保护,这使得使用cURL登录很困难

使用Java我也没有运气尝试直接卷曲的方法和摆弄OpenId参数:

// 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

问题:

在这里使用类似ruby机械化的样式会在Java上运行吗?

1 个答案:

答案 0 :(得分:1)

与Selenium见http://www.seleniumhq.org/ 登录是可以直接的方式。请参阅下面的示例代码

请注意:AmazonUser只是一个帮助类,用于保存登录所需的元素:

  • 电子邮件
  • 密码

使用示例

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

亚马逊助手类

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();      
  }


}
相关问题