【Java爬蟲-模擬淘寶登陸】

 已失效!
1
package demo2; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebElement; 5 import org.openqa.selenium.firefox.FirefoxDriver; 6 import org.openqa.selenium.firefox.FirefoxOptions; 7 import org.openqa.selenium.firefox.FirefoxProfile; 8 import java.io.File; 9 import java.util.List; 10 import java.util.Random; 11 12 13 /** 14 * 模擬登陸淘寶的,用正常的方法登陸太難了,只能經過這種方法進行登陸。 15 * @author iitxt 16 * 17 */ 18 public class Test2 { 19 public static void main(String[] args) { 20 System.setProperty("webdriver.firefox.bin", "D:\\Firefox\\firefox.exe"); 21 System.setProperty("webdriver.gecko.driver", "C:\\U\\geckodriver.exe"); 22 FirefoxOptions options = new FirefoxOptions(); 23 FirefoxProfile profile = new FirefoxProfile( 24 new File("C:\\Users\\Aaron_Kevin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ai2t1t79.default")); 25 options.setProfile(profile); 26 FirefoxDriver driver = new FirefoxDriver(); 27 driver.get("https://login.m.taobao.com/login.htm"); 28 // 下面開始徹底模擬正常人的操做,因此你會看到不少 sleep 操做 29 WebElement usernameElement = driver.findElement(By.id("username")); 30 // 模擬用戶點擊用戶名輸入框 31 usernameElement.click(); 32 String username = "xxxx";// 你的手機號 33 String password = "xxxxx";// 你的密碼 34 Random rand = new Random(); 35 try { 36 for (int i = 0; i < username.length(); i++) { 37 Thread.sleep(rand.nextInt(1000));// 隨機睡眠0-1秒 38 // 逐個輸入單個字符 39 usernameElement.sendKeys("" + username.charAt(i)); 40 } 41 WebElement passwordElement = driver.findElement(By.id("password")); 42 passwordElement.click(); 43 // 輸入完成用戶名後,隨機睡眠0-3秒 44 Thread.sleep(rand.nextInt(3000)); 45 for (int i = 0; i < password.length(); i++) { 46 Thread.sleep(rand.nextInt(1000)); 47 passwordElement.sendKeys("" + password.charAt(i)); 48 } 49 driver.findElement(By.id("btn-submit")).click(); 50 Thread.sleep(rand.nextInt(1000)); 51 //模擬點擊登陸電腦版 52 WebElement aboutLink = driver.findElement(By.linkText("電腦版")); 53 aboutLink.click(); 54 } catch (Exception e) { 55 e.printStackTrace(); 56 } 57 58 try { 59 Thread.sleep(300000); 60 } catch (InterruptedException ie) { 61 ie.printStackTrace(); 62 } 63 64 driver.quit(); 65 } 66 }

 已失效!

相關文章
相關標籤/搜索