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自动抢票的更多相关文章

  1. python之12306自动查票

      一.导读 本篇文章所采用的技术仅用于学习.研究,任何其他用途请自行承担后果. 12306自动查票使用到的python库主要是splinter,同时也涉及到查票的城市编码,具体的城市编码请在网络上搜 ...

  2. python+selenium实现自动抢票

    使用说明 程序运行开始,需要输入出发地,目的地,出发时间,乘客信息,车次:乘客信息和车次可以输入多个 刚刚开始学习爬虫,selenium仅仅是解放了双手,运行效率不是很高: 程序运行时会打开chrom ...

  3. c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇

    这一篇文章,我将从头到尾教大家使用c#模拟网页面登陆12306网站,自动刷票,选择订票人,到最后一步提交订单.研究过HTTP协议的童鞋们都知道,我们在访问网站时,是有两种方式的,POST和GET方式, ...

  4. Python实例--12306的抢票功能

    基础知识学习 目标: 通过python程序实现自动登录下单功能 知识点: Selenium + 云打码 + Python 学习链接: 1. Python学习--Selenium模块 2. Python ...

  5. 抢票季:吐槽12306 & 分享抢票经验

    又是一年一度的春运抢票季,不管你是北上.南下或者东进,在外漂泊了一年,有钱没钱总是要回家过年的. [图片来源于网络] 吐槽:12306抢票的悲伤 据说12306改版了,新版本里面除了UI这些面儿上的改 ...

  6. python自动抢票

    # -*- coding: utf-8 -*- from splinter.browser import Browser from time import sleep import traceback ...

  7. 360自动抢票还不够,几行js代码设置无人值守

    360就是牛逼哄哄的...... 但是最近在使用360浏览器抢票的时候还是发现了一些体验不好的地方,比如搞着搞着就退出了登录,有时候能帮你自动登录进去,但是自动登录之后又不会帮你自动开始抢.然后验证码 ...

  8. 12306 Pytho抢票代码

    1.需要先安装python环境 2.安装selenium模拟用户来操作浏览器 3.将chromedriver驱动放入chrome浏览器应用根目录 4.用文本编辑器打开脚本,编辑购票人信息 5.通过cm ...

  9. Python实现12306自动查票程序

    这是在网上扒拉过来的,原文链接: http://blog.csdn.net/An_Feng_z/article/details/78631290 目前时间2018/01/04 文中各种接口均为可用,亲 ...

随机推荐

  1. 【总结】性能调优:JVM内存调优相关文章

    [总结]性能调优:JVM内存诊断工具 [总结]性能调优:CPU消耗分析 [总结]性能调优:消耗分析 JVM性能调优

  2. tensorflow:typeerror:‘noneType’ object is not callable

    程序运行报错 typeerror: ‘noneType’ object is not callable 解决方法:删除缓存文件,再次运行没有错误 删除__pycache__文件夹

  3. Django的模型层(2)---多表操作

    多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...

  4. java 解析excle

    jjava解析excle或者csv文件并导出到web界面: 创建ExcelShower.java package com.ssm.controller; import java.io.File; im ...

  5. 3D 图片播放焦点图插件Adaptor

    在线演示 本地下载

  6. chunkhash笔记

    假设有main1.main2两个入口文件,main引入chunk1.chunk2,main2引入chunk1 改变chunk2 main1的chunkhash改变,main2不发生改变 main再引入 ...

  7. WINFROM中自定义控件之绑定数据即时更新

    相信在WINFROM中写自定义控件或者用户控件,很多人都多多少少用过点 最近发现一个用户控件,绑定的数据源没办法自动更新,其实以前处理过这类的问题,可是这次遇到又花了1个多小时,所以决定记下来 在用户 ...

  8. 【html学习整理】常用标签

    什么是html 超文本标记语言 html语法规则       所有的命令放到<> 大部分成对存在,以<tag>开始,</tag>结束 网页的基本框架,常用的标记 & ...

  9. js/jq 动态添加的元素不能触发绑定事件解决方案

    <!-- Copyright 2017-10-27, Jachin QQ: 381558301 Email: 381558301@qq.com 请看看你们的版本并对号入座: jquery1.6版 ...

  10. PyNLPIR python中文分词工具

    官网:https://pynlpir.readthedocs.io/en/latest/  github:https://github.com/tsroten/pynlpir          NLP ...