package com.lkb.start; import com.alibaba.fastjson.JSONObject; import com.lkb.bean.Entity; import com.lkb.bean.ResOutputTaobao; import com.lkb.util.ThreadUtil; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import java.util.Scanner; public class TaobaoLogin { public static void main(String[] args) { WebDriverUtil page = new WebDriverUtil(); page.setManager(new WebDriverManager()); Entity entity = new Entity(); entity.setLoginName("****"); entity.setPassword("****"); WebDriver driver = page.get(); Actions actions = new Actions(driver); page.setEntity(entity); openUrl(page); setLoginName(page); boolean isShow = showSlider(page); if (isShow) { slider(page); try { ResOutputTaobao res = getImgUrl(page); System.out.println(res); } catch (Exception e) { e.printStackTrace(); } } // 点击坐标 System.out.println("请输入X轴坐标:"); Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); System.out.println("请输入Y轴坐标:"); scanner = new Scanner(System.in); int y = scanner.nextInt(); boolean isCorrect = clickAuthCode(page,actions,x,y); if(isCorrect){ setPassword(page); doLogin(page); } System.out.println(driver.getPageSource()); System.out.println(driver.manage().getCookies()); // driver.quit(); System.out.println("执行结束了"); } /** * 点击图片验证码 * @param page * @param actions * @param x * @param y * @return */ public static boolean clickAuthCode(WebDriverUtil page,Actions actions,int x,int y){ WebElement element = page.get().findElement(By.className("clickCaptcha_img")).findElement(By.tagName("img")); actions.moveToElement(element,x,y).click().perform(); // 解析状态 return false; } public static void openUrl(WebDriverUtil page) { page.get().get("https://login.taobao.com"); page.pageRender(); } public static void setLoginName(WebDriverUtil page) { ThreadUtil.sleep(1.1f); WebElement element = page.get().findElement(By.id("TPL_username_1")); element.clear(); element.sendKeys(page.getEntity().getLoginName()); page.pageRender(); // 切换焦点 element = page.get().findElement(By.id("TPL_password_1")); element.click(); ThreadUtil.sleep(1); } public static void setPassword(WebDriverUtil page) { WebElement element = page.get().findElement(By.id("TPL_password_1")); element.clear(); element.sendKeys(page.getEntity().getPassword()); page.pageRender(); ThreadUtil.sleep(2); page.pageRender(); } public static boolean showSlider(WebDriverUtil page) { WebElement element = page.get().findElement(By.id("nocaptcha")); // System.out.println(element.isDisplayed()); // ((JavascriptExecutor)page.get()).executeScript("document.getElementById("nocaptcha").style.display='block'"); // ThreadUtil.sleepMillis(300l); return element.isDisplayed(); } public static void slider(WebDriverUtil page) { WebElement element = page.get().findElement(By.id("_bg")); System.out.println(element.getLocation().getX() + "----" + element.getLocation().getY()); Actions action = new Actions(page.get()); // 鼠标拖拽动作,将 source 元素拖放到 (xOffset, yOffset) 位置,其中 xOffset 为横坐标,yOffset 为纵坐标。 int x = element.getLocation().getX() + 1; for (int i = 0; i < 15; i++) { // action.dragAndDropBy(element,1139,430).perform(); action.dragAndDropBy(element, x, 430).perform(); x = x + 1; } // action.dragAndDropBy(element,1200,430).perform(); // action.dragAndDropBy(element,1309,430).perform(); // page.pageRender(); action.release();// 释放鼠标 page.pageRender(); } /** * 0不需要,1点击验证码,2图片验证码 */ public static ResOutputTaobao getImgUrl(WebDriverUtil page) { ResOutputTaobao obj = new ResOutputTaobao(); String sty1 = page.get().findElement(By.id("clickCaptcha")).getCssValue("display"); String sty2 = page.get().findElement(By.id("imgCaptcha")).getCssValue("display"); System.out.println(page.get().getPageSource()); if (sty1 != null && "block".equals(sty1)) { obj.setIsHasImg(1); String url = page.get().findElement(By.className("clickCaptcha_img")).findElement(By.tagName("img")).getAttribute("src"); obj.setUrl(url); String text = page.get().findElement(By.id("_scale_text")).getText(); if (text != null) { text = text.replace("<i>", ""); } obj.setImgText(text); } else if (sty2 != null && "block".equals(sty2)) { obj.setIsHasImg(2); String url = page.get().findElement(By.id("_imgCaptcha_img")).findElement(By.tagName("img")).getAttribute("src"); obj.setUrl(url); } page.setAuthCodeMethod(obj.getIsHasImg()); return obj; } public static void clickSubmit(WebDriverUtil page, ResOutputTaobao res) { String value = page.get().findElement(By.id("J_NcoToken")).getAttribute("value"); String value1 = null; // long startTime = System.currentTimeMillis(); page.get().findElement(By.id("J_SubmitStatic")).click(); WebElement ele; for (int i = 0; i < 100; i++) { ele = page.get().findElement(By.id("J_NcoToken")); if (ele != null) { value1 = ele.getAttribute("value"); if (value1 != null && !value1.equals(value)) { System.out.println(page.get().getPageSource()); break; } ThreadUtil.sleep(400l); } else { ThreadUtil.sleep(400l); } } } public static boolean doLogin(WebDriverUtil page) { boolean isNeedPhoneCode = false; WebElement element = page.get().findElement(By.id("J_SubmitStatic")); element.click(); return isNeedPhoneCode; } public static void setAuthCode(WebDriverUtil page) { String authCode = page.getEntity().getAuthCode(); if (page.getAuthCodeMethod() == 1) { try { //淘宝传送到后台是230 JSONObject json = JSONObject.parseObject(authCode); float w = json.getFloatValue("w"); float h = json.getFloatValue("h"); float x = json.getFloatValue("x"); float y = json.getFloatValue("y"); float w_b = w / 230; float h_b = h / 230; int x_result = (int) (x / w_b); int y_result = (int) (y / h_b); WebElement webElement = page.get().findElement(By.className("clickCaptcha_img")); webElement = webElement.findElement(By.tagName("img")); Actions action = new Actions(page.get()); action.moveByOffset(webElement.getLocation().x + x_result, webElement.getLocation().y + y_result).perform(); System.out.println((webElement.getLocation().x + x_result) + "====" + (webElement.getLocation().y + y_result)); System.out.println((webElement.getLocation().x) + "====" + (webElement.getLocation().y)); webElement.click(); action.release();// 释放鼠标 ThreadUtil.sleep(1); page.pageRender(); // page.get().findElement(By.id("_btn_2")).click(); webElement = page.get().findElement(By.id("_btn_2")); System.out.println((webElement.getLocation().x) + "===" + (webElement.getLocation().y)); new Actions(page.get()).moveToElement(webElement).perform(); webElement.click(); page.pageRender(); } catch (Exception e) { e.printStackTrace(); } } else if (page.getAuthCodeMethod() == 2) { } } public static boolean isCountine(WebDriverUtil page) { boolean b = false; for (int i = 0; i < 10; i++) { if (page.getType() > 0) { ThreadUtil.sleep(500l); } else { if (page.isLogin()) { b = true; } break; } } return b; } }