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. 算法系列:CSAPP 推荐

    转载自:https://book.douban.com/review/6093947/ 如果你觉得这本书过于厚重担心看不下来的话,不妨跟着coursera的Hardware/Software Inte ...

  2. git 本地仓库和远程仓库及本地分支和远程分支

    从远程git仓库签出代码:   $ git clone git://aaa.com/git_project.git (远程git服务器项目所在地址) 当你需要克隆远程项目到本地时,默认会把项目保存在名 ...

  3. Linux光纖卡配置,磁盤掛載,多路徑設置

    Linux光纖卡配置 1.首先根據光纖卡類型加載對應的驅動.我這裡常用的是QLogic和Brocade光纖卡 [root@rhcsasm2 host3]# lspci | grep Fibre   - ...

  4. [Eclipse][SVN] 在eclipse上安装SVN

    以前装过好多次SVN,始终没有一次把安装过程记录下来,这次新装机器,安装SVN插件时一波三折,记录下来免得以后又忘记了.   方法一: 1. 直接通过后台添加URL通过互联网进行安装,直接上图: 2. ...

  5. POJ 3241 Object Clustering 曼哈顿最小生成树

    Object Clustering   Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...

  6. 自己yy的Splay

    #include <iostream> #include <cstdio> #include <queue> using namespace std; ; stru ...

  7. memcached for windows 修改端口和最大内存,以及常用命令

    在windows中使用memcached,必须先下载memcached for win32安装. PHP模块MemCache下载地址:http://downloads.php.net/pierre 服 ...

  8. supervisor简介

    一什么是supervisor Superviosr是一个UNIX-like系统上的进程监控工具. Supervisor是一个Python开发的client/server系统,可以管理和监控*nix上面 ...

  9. hdu1963 完全背包(数据压缩)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1963 注意:题中有一句话说债券的价钱都是1000的倍数,我之前没看到这句话,写的完全背包, ...

  10. 车销送货上门专用无线开单器-自带PDA无线移动开单系统 与云服务器连接

    浩瀚技术配套PDA终端软件 本软件与 数据采集器搭配销售,PDA端软件不单独销售也不含电脑端管理软件 数据采集器 一维扫描头+WIFI+蓝牙+一体打印+PDA软件.  产品特点: 1:通过操作移动手持 ...