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

Selenium_模拟淘宝登录Demo的更多相关文章

  1. 模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能

    Login <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  2. 【Python】selenium模拟淘宝登录

    # -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import By f ...

  3. Python 模拟淘宝登录的两种方法

    方法一.urllib的post登录 import urllib import urllib2 import cookielib def taobao(username,password): cj = ...

  4. 工作任务:模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能

    登入界面<% Cookie[] cks =request.getCookies(); String str=null; for(Cookie ck:cks) { if(ck.getName(). ...

  5. php单点登录之模拟淘宝天猫同步登录

    说到单点登录大家都很了解,一个站点登录其他域会自动登录. 单点登录SSO(Single Sign On)的方法有很多,比如:p3p.共享session.共享cookice.第三方OAuth认证. 这里 ...

  6. 模拟淘宝购物,运用cookie,记录登录账号信息,并且记住购物车内所选的商品

    1.登录界面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...

  7. SSH框架实现仿淘宝购物demo

    还记得六月份实习的时候,曾经做过一个电商的项目,项目里面需要实现类似淘宝购物车的移动端的demo,随着项目的进行,再一次跟购物车碰面,但是今天呢,不是移动端的需求,如何使用SSH框架实现类似淘宝的购物 ...

  8. selenium模拟淘宝登陆,过所有验证

    淘宝模拟登陆实现 由于淘宝使用了滑动验证码,需要进行模糊手动滑动,因此考虑使用selenium+chromedriver进行模拟登陆. 淘宝的登陆网址:https://login.taobao.com ...

  9. jquery模拟淘宝购物车

    今天要实现的一个功能页面就是利用jquery代码模拟一个淘宝网的购物车结算页面 总体页面效果如图: 首先我们要实现的内容的需求有如下几点: 1.在购物车页面中,当选中“全选”复选框时,所有商品前的复选 ...

随机推荐

  1. Oracle【IT实验室】数据库备份与恢复之六:LogMiner

    6.1 LogMiner 的用途 Oracle LogMiner 是Oracle公司从产品8i以后提供的一个实际非常有用的分析工具,使用该工具可以轻松获得  Oracle 重作日志文件(归档日志文件) ...

  2. 7-14 EXISTS子查询

    EXISTS: 只注重于子查询是否有返回行,如果查有返回行返回结果为值,否则为假 并不使用子查询的结果,仅用于测试子查询是否有返回结果. 语法: IF EXISTS (子查询) BEGIN 语句块 E ...

  3. 自己动手写RTP服务器——关于RTP协议

    转自:http://blog.csdn.net/baby313/article/details/7353605 本文会带领着你一步步动手实现一个简单的RTP传输服务器,旨在了解RTP流媒体传输协议以及 ...

  4. mysql_multi启动数据库

    1.初始化数据库 在$mysql_base目录下,新增加存放data的文件夹,用mysql_install_db命令执行初始化 [root@ora11g scripts]# ./mysql_insta ...

  5. Convert XML to Object using LINQ

    Class and Xml : Please see my another article. http://www.cnblogs.com/mingmingruyuedlut/p/3436803.ht ...

  6. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...

  7. ZOJ 3494 BCD Code(AC自动机+数位DP)

    BCD Code Time Limit: 5 Seconds      Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...

  8. SpringJMS解析3-监听器

    消息监听器容器是一个用于查看JMS目标等待消息到达的特殊bean,一旦消息到达它就可以获取到消息,并通过调用onMessage()方法将消息传递给一个MessageListener实现.Spring中 ...

  9. DSP using MATLAB 示例 Example3.10

    用到的性质 上代码: n = -5:10; x = rand(1,length(n)) + j * rand(1,length(n)); k = -100:100; w = (pi/100)*k; % ...

  10. 端口偷窃(Port Stealing)技术

    端口偷窃(Port Stealing)技术   该技术主要用于局域网中间人攻击中,尤其目标计算机采用静态ARP后,导致ARP欺骗无效.   背景知识:路由器为了方便转发数据包,会在内部记录每个接口和M ...