Qunar机票搜索场景,原文在http://www.cnblogs.com/tobecrazy/p/4752684.html。
现在Qunar的页面有所变更,原文的代码已不能执行。本文修改示例代码后,实现步骤如下:
在Qunar机票首页选择“单程”,输入出发、到达城市,选择7天后的日期,点搜索,跳到机票单程搜索列表页,点击需要转机的航班显示转机信息。每次点击或者输入时,都先调用WebDriverWait判断是否已加载完毕。
实现代码如下:
JAVA代码
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class Qunar {
// 引用原文代码
public static String getDateAfterToday(int dateAfterToday) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, +dateAfterToday);
System.out.println(cal.getTime().toString());
Date date = cal.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(date));
return df.format(date);
}
// 修改后代码
@Test
public void searchTicket() {
System.setProperty("webdriver.chrome.driver", "C:\\work\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://flight.qunar.com/");
driver.manage().window().maximize();
// 出发城市
WebElement from_inpox = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(
driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']"))));
from_inpox.clear();
from_inpox.sendKeys("FOC");
WebElement sour = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//div[@class='qcbox-fixed js-suggestcontainer']//td[contains(text(),'福州')]")));
sour.click();
// 选择单程
WebElement sigleWayCheckBox = driver.findElement(
By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@class='inp_chk js-searchtype-oneway']"));
if (!sigleWayCheckBox.isSelected()) {
sigleWayCheckBox.click();
}
// 到达城市
WebElement to_inpox = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(
driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='toCity']"))));
to_inpox.clear();
to_inpox.sendKeys("NKG");
WebElement dest = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//div[@class='qcbox-fixed js-suggestcontainer']//td[contains(text(),'南京')]")));
dest.click();
// 出发日期
WebElement from_date = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(
driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromDate']"))));
from_date.clear();
from_date.sendKeys(getDateAfterToday(7));
// WebElement search = driver
// .findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//button[@class='btn_search']"));
//
// search.submit();
from_date.submit();
(new WebDriverWait(driver, 10))
.until(ExpectedConditions
.presenceOfElementLocated(By.xpath("//div[@class='content']//div[contains(text(),'停留')]")))
.click();
}
}
###pom.xml
.......
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>
......