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. Linux就该这么学--Shell脚本条件语句(一)

    1.条件测试语句能够让Shell脚本根据实际工作灵活调整工作内容,例如判断系统的状态后执行指定的工作,或创建指定数量的用户,批量修改用户密码,这些都可以让Shell脚本通过条件测试语句完成. if条件 ...

  2. Java for LeetCode 119 Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. LeetCode:寻找旋转排序数组中的最小值【153】

    LeetCode:寻找旋转排序数组中的最小值[153] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0 ...

  4. GIT笔记:将项目发布到码云

    GIT笔记:将项目发布到码云 发布步骤 1.码云创建项目 记录下项目的远程地址: https://gitee.com/mrsaber/ms_supplyAndSale.git 2.在本地创建GIT仓库 ...

  5. SVG圆盘时钟动画

    在线演示 本地下载

  6. ScrollView当显示超出当前页面时自动移动到最底端【转】

    本文转载自:http://gundumw100.iteye.com/blog/1162964 卷轴视图(ScrollView)是指当拥有很多内容,一屏显示不完时,需要通过滚动来显示视图.比如在做一个阅 ...

  7. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  8. JAVA- String类练习

    JAVA- String类练习 需求1:去除字符串两边空格的函数,写一个自己的trim(); public class TestTrim { public static void main(Strin ...

  9. js的内部类

    JavaScript中本身提供一些,可以直接使用的类,这种类就是内部类.主要有: Object/Array/Math/Boolean/String/RegExp/Date/Number共8个内部类. ...

  10. css(3)

    行内元素和块内元素可以通过定义display的属性值进行相互转换. 想要叫ul中的li实现横向显示可以在li中加入布局"float:left": 如: /*这个用于控制单个图片区域 ...