foreword
Unintentionally, I found a small demo of Taobao spike based on Python in the forum. I thought it was very interesting, so I wrote it in Python.
# 淘宝清空购物车装置
import time
# 新版的selenium和老版的有区别,坑死我了
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
ShoppingTime = "19:00"
# 先登录
driver.get("https://login.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F/")
time.sleep(15)
# 进入购物车
driver.get("https://cart.taobao.com/cart.htm?from=mini&ad_id=&am_id=&cm_id=&pm_id=1501036000a02c5c3739")
time.sleep(2)
# 选中全选
all = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div/div/div[2]/div[1]/div/div['
'1]/div/div/label')
if all :
all.click()
time.sleep(1)
buy = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div/div/div[4]/div[2]/div[3]/div[5]/a')
Later, when I wrote the time judgment, I found that the time format had to be rotated. I was really unfamiliar with Python. I thought that openqa seemed to have selenium, so I tried to write it in Java. After a simple search, there were not many people who wrote this in Java. Make a fuss on your own, but when it comes to reptiles, it is recommended to use Python as the main language to write, and there are many tutorials in the library, so avoid detours.
The first step is to install ChromeDriver
Everyone uses Chrome by default, download address:
https://sites.google.com/a/chromium.org/chromedriver/home
Because I am in a mac environment, after downloading chromedriver, just copy it directly under /usr/local/bin. When
copying, pay attention to the need for administrator privileges. Just press a fingerprint
and it can be placed in other locations. When writing Java, use code configuration just one click
System.setProperty("webdriver.chrome.driver", "/Users/beamstark/Desktop/chromedriver");
The second step is to guide the package
By default everyone uses Maven
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0-jre</version>
</dependency>
third step test
Java is still handy, just write a demo and try it out
package com;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author BeamStark
* @date 2022-10-08-08:58
*/
public class TEST {
public static void main(String[] args) {
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.get("https://www.baidu.com");
chromeDriver.findElementById("kw").sendKeys("啦啦啦");
chromeDriver.findElementById("su").click();
}
}
Click to run
bingo ~
but at this time the console will report an error (harmless)
that seems to be unsafe, it can only be run locally, and the permissions of the mac have never been understood:(
The fourth step is to use the code to help us buy things
package com;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* 用Java实现淘宝秒杀自动化
* @author BeamStark
* @date 2022-10-08-08:58
*/
@Slf4j
public class WhereIsMyMoney {
private static String ShoppingTime = "2022-10-09 04:02:00";
public static void main(String[] args) throws InterruptedException {
log.info("开始时间:" + LocalDateTime.now());
// 初始化驱动
ChromeDriver chromeDriver = new ChromeDriver();
// 初始化等待时间
WebDriverWait wait15s = new WebDriverWait(chromeDriver,15000);
WebDriverWait wait1s = new WebDriverWait(chromeDriver,1000);
// 先登录
chromeDriver.get("https://login.taobao.com/member/login" +
".jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F/");
chromeDriver.findElementByXPath("/html/body/div/div[2]/div[3]/div/div/div/div[1]/i").click();
log.info("等待登录");
// 进入购物车
wait15s.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[1]/div[1" +
"]/div/ul[2]/li[3]/div[1]/a/span[2]"))).click();
// 选中购物车的第一个
wait1s.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/div[2]/div[1" +
"]/div/div[1]/div/div/label"))).click();
log.info("选中,等待下单");
Thread.sleep(500);
// 等待下单
while (true) {
if (LocalDateTime.now().isAfter(LocalDateTime.parse(ShoppingTime,
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))) {
// 结算!
chromeDriver.findElementByClassName("submit-btn").click();
log.info("结算");
// 锁单
wait1s.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[1" +
"]/div[3]/div/div[1]/div[1]/div/div[9]/div/div/a[2]"))).click();
log.info("锁单");
System.out.println("下单成功,去支付吧! 完成时间:" + LocalDateTime.now());
break;
}
}
// 5秒后关闭
Thread.sleep(5000);
chromeDriver.quit();
}
}
Three consecutive tests passed
the average test time of about 0.8s (related to network speed and page response speed, based on the Chrome kernel, no other browser kernels have been tested), anyway, it’s much faster than manual and you don’t have to worry about it. Playing games and playing games should watch dramas, just hang up~
written in the back
Thanks everyone for coming here :>
This program does not set a payment code for you , it will only lock the order for you (leaving you with room for regret), please spend reasonably
~ wash and sleep: )
Reference article: https://blog.csdn.net/chenjxj123/article/details/121802904