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. sqlplus 中spool命令的简单用法

    spool基本格式: spool 路径+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off ...

  2. 数据结构之图 Part1

    Part 1 预计使用7天的时间来过掉图相关的数据结构.第一天主要是一天图的基本概念,熟练掌握定义是一切交流和沟通的基础. 1定义 1.1图 有穷非空顶点,外加边. G(V,E) Graph Vert ...

  3. 1-02 启动和停止Sql Sever的服务

    启动Sql  Sever服务的三种方式 1:后台启动服务. 2:Sql Sever配置管理员启动服务. 3:在运行窗口中使用命令启动和停止服务: 启动:net start mssqlsever. 停止 ...

  4. hdu 4274 2012长春赛区网络赛 树形dp ***

    设定每个节点的上限和下限,之后向上更新,判断是否出现矛盾 #include<cstdio> #include<iostream> #include<algorithm&g ...

  5. Java 对象序列化(Serialization Object)

    官网文档:https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html 优秀博客: http://www.cnblogs.com/g ...

  6. android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法

      http://blog.csdn.net/fth826595345/article/details/9208771 主题 TextView 转载请注明出处: http://blog.csdn.ne ...

  7. "Project facet Java version 1.7 is not supported"的问题解决的办法

    问题描述 在eclipse中,从SVN中检出project代码,拖拽式部署到local server中的时候,报出以下错误: 问题分析 问题产生的原因是,SVN中的代码是采用java 1.7开发编译的 ...

  8. 在SharePoint2010中用out-of-box的方式自定制Application Pages(AccessDenied,Confirmation,Error,Login,RequestAccess,Signout,WebDeleted)

    在实际项目中需要对SharePoint2010中的AccessDenied页面进行自定制,于是乎上网搜索相关内容,经实际操作此方法可行,便以此文记录. 在SharePoint2010中,由于secur ...

  9. 智能车学习(七)——按键矩阵的实现

    一.原理说明 就是按键矩阵代码书写的一个说明,就是讲K5到K7先输出高电平,而K1和K4则调成上拉输入,如果检测到K1到K4有一个变为0,说明有按键按下去,立刻进行转换,是的K1到K4设置为输出高电平 ...

  10. 导出word使用模版

    在我们做我们的小组项目的时候,刚开始的时候我们用到的是Mvc+EF,用上了我们的ITOO框架.在最开始的计划,我们要用到瑞郎报表.可是呢,由于工期原因以及技术暂时没有实现,我们不得不想一个比较折中的方 ...