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. 扩展LV

    LVM最大的特性就是可以弹性调整磁盘容量下面扩展一个已经存在的LV [root@ol6--rac1 mnt]# lvdisplay --- Logical volume --- LV Path /de ...

  2. windows64系统中mysql64位绿色安装

    将下载压缩包解压到任意目录 配置安装文件: # For advice on how to change settings please see# http://dev.mysql.com/doc/re ...

  3. Ubuntu下快速安装php环境

    今天蛋疼了一下,在Ubuntu下装了一下php的环境,也就是装了一下MySQL.PHP.Apache.话说还真是简单...不禁让我想起原来在windows下开发的时候撑死就是装不上,而且一个就是几个G ...

  4. [Unity3d插件]EasyTouch的初步使用

    对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很 ...

  5. Mac系统修改Intellij Idea默认JDK版本

    Intellij IDEA 默认情况下,使用的jdk的版本是1.6,当第一次启动IDEA的时候,如果系统中未安装jdk,则系统会自动到苹果官网下载jdk安装文件.如果你的系统已经安装了jdk1.7或是 ...

  6. 程序员最喜爱的12个Android应用开发框架二(转)

    在上一篇程序员最喜爱的12个Android应用开发框架(一)中,我们为大家介绍了前6个Android应用开发框架,主要包括了 Xamarin.Phonegap.Corona SDK等.接下来,小编将继 ...

  7. 利用Visual GDB在Visual Studio中进行Android开发

    转载请注明http://www.cnblogs.com/adong7639/p/4119467.html 无意中发现了Visual GDB这个工具,可以再Visual Studio中进行Android ...

  8. BZOJ 1503: [NOI2004]郁闷的出纳员 splay

    1503: [NOI2004]郁闷的出纳员 Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作 ...

  9. Linux学习笔记(16)shell基础之Bash变量

    1. 用户自定义变量 (1)变量设置规则 ① 变量名称可由字母.数字和下划线组成,但不能以数字开头: ② 变量的默认类型为字符串类型,如果要对数值运算,则必须指定变量类型为数值型: ③ 变量用等号连接 ...

  10. 【bootstrapValidator 不验证】使用bootstrapValidator 验证效果不起作用

    虽然在页面ready的时候 就绑定了验证表单 ,但是在点击提交按钮之后 依旧没有验证的效果 . 那就在提交按钮的点击事件中 添加一句话: $(document).ready( function () ...