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());
}
}
}
原文地址:https://www.cnblogs.com/liyunfeng111/p/10283428.html