【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 }

 已失效!

转载于:https://www.cnblogs.com/iitxt/p/9015324.html

你可能感兴趣的:(【Java爬虫-模拟淘宝登录】)