package com.web;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//加载类
public class Example {
public static void test(WebDriver driver) throws InterruptedException {
// 1.打开网页,最大化窗口。
driver.get("http://www.meituan.com");
// 窗口最大化
driver.manage().window().maximize();
// 切换城市
driver.findElement(By.xpath("//*[@id=\"main\"]/header/div[1]/div/div/a")).click();
// 输入框输入“nanjing”
driver.findElement(By.xpath("//*[@id=\"react\"]/div/div[1]/div[2]/input")).sendKeys("nanjing");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id=\"react\"]/div/div[1]/div[2]/div/div/a[1]")).click();
// 点击图片进入酒店预订
driver.findElement(By.xpath("//*[@id=\"banner-slider\"]/div[1]/div[1]/a/div")).click();
String thisHandleId = driver.getWindowHandle();
for (String tempHandleId : driver.getWindowHandles()) {
if (!tempHandleId.equals(thisHandleId)) {
driver.switchTo().window(tempHandleId);
}
}
System.out.println("当前窗口" + driver.getCurrentUrl());
System.out.println("Success--->2.通过首页的住酒店图片链接进入酒店预订页面");
// 入住时间
driver.findElement(By.xpath("//*[@id=\"search-header-placeholder\"]/div/div/div[2]/div[1]/div[1]/input")).click();
String month = "";
while (!month.equals("10")) {
month = driver.findElement(By.xpath("//*[@id=\"search-header-placeholder\"]/div/div/div[2]/div[1]/div[2]/div/span/input[2]")).getAttribute("value");
driver.findElement(By.xpath("//*[@id=\"search-header-placeholder\"]/div/div/div[2]/div[1]/div[2]/div/a[2]")).click();
System.out.println("current Month:" + month);
}
// 选择date
driver.findElement(By.xpath("//*[@id=\"search-header-placeholder\"]/div/div/div[2]/div[1]/div[2]/table/tbody/tr[3]/td[3]")).click();
// 下一个时间选择
driver.findElement(By.xpath("//*[@id=\"search-header-placeholder\"]/div/div/div[2]/div[2]/div[2]/table/tbody/tr[4]/td[6]")).click();
// 位置
driver.findElement(By.xpath("//*[@id=\"app\"]/section/section/div[2]/div[1]/div[1]/div[3]/div[5]/span")).click();
driver.findElement(By.xpath("//*[@id=\"app\"]/section/section/div[2]/div[1]/div[2]/div[5]/div[1]/a[1]")).click();
// 经济
driver.findElement(By.xpath("//*[@id=\"app\"]/section/section/div[2]/div[2]/div/div[3]/span[1]/a/label")).click();
driver.findElement(By.xpath("//*[@id=\"app\"]/section/section/div[2]/div[3]/div/div[3]/span[1]/a/label")).click();
driver.findElement(By.xpath("//*[@id=\"app\"]/section/section/div[2]/div[4]/div/div[2]/a")).click();
// 关闭广告
driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]")).click();
// 点击查看详情
driver.findElement(By.xpath("//*[@id=\"list-view\"]/div[1]/article[1]/div[2]/h3/a")).click();
// 切入新窗口
thisHandleId = driver.getWindowHandle();
for (String tempHandleId : driver.getWindowHandles()) {
if (!tempHandleId.equals(thisHandleId)) {
driver.switchTo().window(tempHandleId);
}
}
}
// 主函数
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
try {
test(driver);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("即将关闭ChromeDriver...");
System.out.println("三秒等待中...");
Thread.sleep(3000);
driver.quit();
System.out.println("已退出程序。。。");
}
}
// 主函数
}
// 加载类