基于selenium+java的12306自动抢票
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Ticket12306 {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", "tools\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc");
try {
Thread.sleep(1000);
WebElement button1 = driver.findElement(By.cssSelector("#J-header-login > a:nth-child(1)")); //登录按钮
button1.click();
Thread.sleep(1000);
WebElement button2 = driver.findElement(By.cssSelector("body > div.login-panel > div.login-box > ul > li.login-hd-account > a")); //账号登录按钮
button2.click();
Thread.sleep(1000);
WebElement box1 = driver.findElement(By.id("J-userName"));//账号文本框
box1.sendKeys(" ");//这里输入账号
WebElement box2 = driver.findElement(By.id("J-password")); //密码文本框
box2.sendKeys(" ");//这里输入密码
Thread.sleep(5000);
WebElement button3 = driver.findElement(By.id("J-login")); //立即登录按钮
button3.click();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
grabbing(driver);
} catch (Exception e) {
driver.navigate().refresh();
grabbing(driver);
}
}
public static void grabbing(WebDriver driver) {
try {
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.cssSelector("#J-chepiao > a"))).perform();// 模式鼠标悬浮
Thread.sleep(5000);
WebElement button4 = driver.findElement(By.cssSelector("#J-chepiao > div > div:nth-child(1) > ul > li.nav_dan > a")); //单程按钮
button4.click();
WebElement box3 = driver.findElement(By.id("fromStationText")); //出发地文本框
box3.click();
box3.sendKeys("杭州东");
box3.sendKeys(Keys.ENTER);
WebElement box4 = driver.findElement(By.id("toStationText")); //目的地文本框
box4.click();
box4.sendKeys("抚州东");
box4.sendKeys(Keys.ENTER);
WebElement box5 = driver.findElement(By.id("train_date")); //出发日期文本框
box5.click();
// Thread.sleep(2000);
WebElement box6 = driver.findElement(By.cssSelector("body > div.cal-wrap > div.cal.cal-right > div.cal-cm > div:nth-child(2) > div"));//具体出发日期
box6.click();
WebElement button5 = driver.findElement(By.id("query_ticket"));//查询按钮
button5.click();
WebElement button6 = driver.findElement(By.cssSelector("#ZE_5l000G479370 > div"));//第一班一等座元素
boolean buttonVal = button6.getText().equals("无");
int count = 1;
while (buttonVal) {
try {
button5 = driver.findElement(By.id("query_ticket"));//查询按钮
button5.click();
count++;
button6 = driver.findElement(By.cssSelector("#ZE_5l000G479370 > div"));
buttonVal = button6.getText().equals("无");
if (!buttonVal) {
break;
}
} catch (Exception e) {
driver.navigate().refresh();
System.out.println("报错了。。。。");
}
}
System.out.println("第 " + count + "次终于抢到了!!!");
WebElement button7 = driver.findElement(By.cssSelector("#ticket_5l000G479370 > td.no-br > a")); //预订按钮
button7.click();
WebElement button8 = driver.findElement(By.cssSelector("#normal_passenger_id > li:nth-child(1) > label")); //选择买票人
button8.click();
WebElement button9 = driver.findElement(By.id("submitOrder_id"));//提交订单
button9.click();
Thread.sleep(3000);
WebElement button10 = driver.findElement(By.cssSelector("#qr_submit_id")); //弹窗确认按钮
button10.click();
System.out.println("抢到票了!!!");
} catch (Exception e) {
System.out.println("错了:" + e.getLocalizedMessage());
}
}
}
基于selenium+java的12306自动抢票的更多相关文章
- python之12306自动查票
一.导读 本篇文章所采用的技术仅用于学习.研究,任何其他用途请自行承担后果. 12306自动查票使用到的python库主要是splinter,同时也涉及到查票的城市编码,具体的城市编码请在网络上搜 ...
- python+selenium实现自动抢票
使用说明 程序运行开始,需要输入出发地,目的地,出发时间,乘客信息,车次:乘客信息和车次可以输入多个 刚刚开始学习爬虫,selenium仅仅是解放了双手,运行效率不是很高: 程序运行时会打开chrom ...
- c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇
这一篇文章,我将从头到尾教大家使用c#模拟网页面登陆12306网站,自动刷票,选择订票人,到最后一步提交订单.研究过HTTP协议的童鞋们都知道,我们在访问网站时,是有两种方式的,POST和GET方式, ...
- Python实例--12306的抢票功能
基础知识学习 目标: 通过python程序实现自动登录下单功能 知识点: Selenium + 云打码 + Python 学习链接: 1. Python学习--Selenium模块 2. Python ...
- 抢票季:吐槽12306 & 分享抢票经验
又是一年一度的春运抢票季,不管你是北上.南下或者东进,在外漂泊了一年,有钱没钱总是要回家过年的. [图片来源于网络] 吐槽:12306抢票的悲伤 据说12306改版了,新版本里面除了UI这些面儿上的改 ...
- python自动抢票
# -*- coding: utf-8 -*- from splinter.browser import Browser from time import sleep import traceback ...
- 360自动抢票还不够,几行js代码设置无人值守
360就是牛逼哄哄的...... 但是最近在使用360浏览器抢票的时候还是发现了一些体验不好的地方,比如搞着搞着就退出了登录,有时候能帮你自动登录进去,但是自动登录之后又不会帮你自动开始抢.然后验证码 ...
- 12306 Pytho抢票代码
1.需要先安装python环境 2.安装selenium模拟用户来操作浏览器 3.将chromedriver驱动放入chrome浏览器应用根目录 4.用文本编辑器打开脚本,编辑购票人信息 5.通过cm ...
- Python实现12306自动查票程序
这是在网上扒拉过来的,原文链接: http://blog.csdn.net/An_Feng_z/article/details/78631290 目前时间2018/01/04 文中各种接口均为可用,亲 ...
随机推荐
- PECL的安装和使用
下载并安装pear脚本 cd /usr/local/php/bin/ curl -o go-pear.php http://pear.php.net/go-pear.phar ./php go-pea ...
- 我的Android进阶之旅------>Android资源文件string.xml中\u2026的意思
今天看了一个string.xml文件,对其中的一行代码中包含的\u2026不是很理解,后来查阅资料后发现了其中的意思. 代码如下: <resources xmlns:xliff="ur ...
- 怎样拆分View Controller进而实现轻量级的View Controller[UIKit]
參考文章来自objcio站点 为什么要编写轻量级的View Controller?? 1.作为iOS项目中最大的文件,ViewControllers中的代码复用率差点儿是最低的 2.重量级的V ...
- 第一个Vert.x程序
Jar依赖 <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</ar ...
- 剑指Offer:删除链表的节点【18】
剑指Offer:删除链表的节点[18] 题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3-& ...
- 【css学习整理】css基础(样式,语法,选择器)
CSS是什么? cascading 层叠样式表 sheet 样式文件 style 外观个性化 CSS语法? 声明方法: 选择器(属性: 值; 属性: 值) 选择器: 通过名称制定对哪些标签进行样式设置 ...
- 谷歌新操作系统fuchsia
开源地址: https://github.com/fuchsia-mirror
- ActorModel 概念翻译
学习 skynet 时初次接触到 ActorModel 模型,始终觉得有必要从宏观上了解 ActorModel 的概念,所以以维基上这篇文章为参考,把文章中的部分内容翻译成中文,好让自己体会一下 Ac ...
- arm-linux-gcc4.4.3编译busybox-1.25.0
系统环境: 1.操作系统:Ubuntu16.04 2.交叉编译工具链:arm-linux-gcc4.4.3 3.busybox源码包:busybox-1.25.0 一.修改Makefile配置 首先解 ...
- Java 的序列化Serializable接口介绍及应用
常看到类中有一串很长的 如 private static final long serialVersionUID = -4667619549931154146L;的数字声明.这些其实是对此类进行序列化 ...