java實現淘寶模擬登陸


java實現淘寶模擬登陸

一、前期准備

1. 工具

  1. IDE(筆者使用IDEA )

  2. Maven

  3. 瀏覽器(筆者使用Chrome)

2. 下載瀏覽器驅動

​看你使用什么瀏覽器了,下載對應的驅動即可,此處以chrome為例,其他瀏覽器見此博客https://www.cnblogs.com/janson071/p/10439078.html

官方下載地址http://chromedriver.storage.googleapis.com/index.html

感謝阿里開源計划,國內的用戶也可以去淘寶鏡像下載,http://npm.taobao.org/mirrors/chromedriver

注意:版本需要和你的瀏覽器版本一致。

下載好之后解壓即可,放到一個好找的地方。

3. 導入Maven依賴

本次項目基於Selenium,官方網址https://www.selenium.dev/,打不打得開就看運氣了。

中文網http://www.selenium.org.cn/,這個倒是打得開,不過沒有官網的全面。

創建好maven項目導入下面依賴

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.14.0</version>
        </dependency>

        <!-- 與 selenium-java 版本要一致 -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.14.0</version>
        </dependency>

二、具體代碼

package ink.zerohua;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.util.Random;

public class TaoBaoLogin {
  
  public static void main(String[] args) {
    //此處填寫驅動程序,前面是驅動名隨瀏覽器改變,后面是你對應驅動程序的文件位置,我是絕對路徑寫法
    System.setProperty(
        "webdriver.chrome.driver",
        "D:\\ideaProjects\\imitate_taobao_login\\src\\main\\resources\\driver\\ChromeDriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    //此處填寫需要打開的網址,這是淘寶的登錄網址
    driver.get(
        "https://login.taobao.com/member/login.jhtml");
    // 你的用戶名
    String username = "your username";
    // 你的密碼
    String password = "your password";
    //通過選擇器獲取元素
    WebElement usernameElement = driver.findElement(By.id("fm-login-id"));
    // 模擬用戶點擊用戶名輸入框
    usernameElement.click();
	// 設置sleep,方便觀察
    Random rand = new Random();
    try {
      for (int i = 0; i < username.length(); i++) {
        // 隨機睡眠0-1秒
        Thread.sleep(rand.nextInt(1000));
        // 逐個輸入單個字符
        usernameElement.sendKeys("" + username.charAt(i));
      }
      WebElement passwordElement = driver.findElement(By.id("fm-login-password"));
      passwordElement.click();
      // 輸入完成用戶名后,隨機睡眠0-3秒
      Thread.sleep(rand.nextInt(3000));
      for (int i = 0; i < password.length(); i++) {
        Thread.sleep(rand.nextInt(1000));
        passwordElement.sendKeys("" + password.charAt(i));
      }
      Actions action = new Actions(driver);
      WebElement moveButton = driver.findElement(By.id("nc_1_n1z"));
      // 移到滑塊元素並懸停,不能超出框的長度,否則異常
      action.clickAndHold(moveButton);
      action.moveByOffset(258, 0).perform();
      action.release();
         
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      Thread.sleep(300000);
    } catch (InterruptedException ie) {
      ie.printStackTrace();
    }
    driver.quit();
  }
  
}

WebElement usernameElement = driver.findElement(By.id("fm-login-id"));

這個就和js一樣,通過選擇器獲取元素,具體請看http://www.selenium.org.cn/1904.html

2020.6.9日測試成功,不知道以后會不會改。

如有錯誤或者建議,還請指出。

三、參考:

https://www.cnblogs.com/janson071/p/10439078.html

https://ask.csdn.net/questions/715089

https://www.cnblogs.com/iitxt/p/9015324.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM