1.对出发地方和到达地方进行Xpath定位,这里采用了Xpath的text() 函数进行定位

用following::sibling选择当前元素后的兄弟元素,比如出发框的Xpath表达式如下:

.//*[text()= '出发']/following-sibling::input"

也可以用下面Xpath

//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']

 /**
* @author Hjianhui
* 12306 2016-08-05
*
*/
public class test{ public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); try{
driver.get("http://www.qunar.com/");
driver.manage().window().maximize();
//输入出发地方
driver.findElement(By.xpath(".//*[text()= '出发']/following-sibling::input")).clear();
driver.findElement(By.xpath(".//*[text()= '出发']/following-sibling::input")).sendKeys("北京"); ////输入到达地方
driver.findElement(By.xpath(".//*[text()= '到达']/following-sibling::input")).clear();
driver.findElement(By.xpath(".//*[text()= '到达']/following-sibling::input")).sendKeys("广州"); if(isElementPresent(driver, By.xpath(".//*[@id='closeXI20']")))
driver.findElement(By.xpath(".//*[@id='closeXI20']")).click(); driver.findElement(By.id("js_domestic_fromdate")).clear();
driver.findElement(By.id("js_domestic_fromdate")).sendKeys("2016-08-20"); //点击立即搜索
driver.findElement(By.xpath(".//span[@class = 'p_btn']/button")).click(); }catch (Exception e){
e.printStackTrace();
}
driver.quit();
} public static boolean isElementPresent(WebDriver driver, By by) {
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}

由于在输入到达地方的时候,可能会有下图挡住了立即搜索,导致立即搜索不可见,因此用isElementPresent()函数判断该图下的右上角图标是否出现,如果出现,点击取消他,然后再点击立即搜索

Selenium Webdriver——去哪儿网输入实例的更多相关文章

  1. 去哪儿网输入框三种输入方式(selenium webdriver 干货)

    在机票预定的页面,输入出发城市和到达城市输入框的时候, 发现直接使用sendkeys不好使, 大部分情况出现输入某城市后没有输入进去, 经过几天的研究,发现可以采取三种方式: 1. 先点击输入框,待弹 ...

  2. 输入框三种输入方式(selenium webdriver 干货)

    在机票预定的页面,输入出发城市和到达城市输入框的时候, 发现直接使用sendkeys不好使, 大部分情况出现输入某城市后没有输入进去, 经过几天的研究,发现可以采取三种方式: 1. 先点击输入框,待弹 ...

  3. Selenium WebDriver TestNg Maven Eclipse java 简单实例

    环境准备 前提条件Eclipse 已经安装过 TestNg ,Maven 插件 新建一个普通的java项目 点击右键 configure->convert to Maven Project 之后 ...

  4. selenium webdriver启动Chrome浏览器后无法输入网址的解决办法

    通过selenium webdriver启动Chrome浏览器,脚本如下: from selenium import webdriver browser = webdriver.Chrome() br ...

  5. Selenium WebDriver java 简单实例

    开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http ...

  6. selenium webdriver——JS对Input执行输入

    直接设置value属性, 此方法主要应对输入框自动补全以及readonly属性的element,sendkeys不稳定 比如: Demo:用JS输入 import org.openqa.seleniu ...

  7. (java)selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作、通过tagname查找元素

    selenium webdriver学习--通过id.name定位,输入内容,搜索,关闭操作:通过tagname查找元素 打开谷歌浏览器,输入不同的网站,搜索框的定位含有不同元素(有时为id,有时为n ...

  8. 一个selenium笔试题——去哪网首页获取符合要求的url并保存

    今天在群里看到这样一个笔试题:请使用任何熟悉的面向对象编程语言,编写代码,获取http://www.qyer.com页面中,所有</a>标签"href"属性值包含英文单 ...

  9. selenium webdriver (python)的基本用法一

    阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...

随机推荐

  1. Arrays.copyof(···)与System.arraycopy(···)区别

    首先观察先System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)的声明: public stati ...

  2. IOS-高仿bilibili项目

    高仿bilibili项目成长之路 (logo) 高仿bilibili项目 Github链接:(https://github.com/MichaelHuyp/Bilibili_Wuxianda) 目前完 ...

  3. 浅析promise

    Promise是一个构造函数,可以通过new 操作符获取一个promise对象,promise者,人如其名也.对,就是承诺.显示生活中,我们承诺别人一件事,一般会在将来某个时间兑现承诺.而 Promi ...

  4. spring boot 使用velocity模板(十六)

    (不要使用这种模板了,spring boot最新版已经不支持了.使用FreeMarker吧:http://blog.csdn.net/clementad/article/details/5194262 ...

  5. 041——VUE中组件之pros数据的多种验证机制实例详解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. reg文件中文乱码

    用reg文件方式修改注册表很方便,但是如果字段中有中文的话,执行reg文件后,注册表中的中文出现乱码. 解决办法:将文件保存文Unicode编码方式即可.(保存文UTF8编码也是不行的) 附一个添加\ ...

  7. 快速切题 poj 2993 Emag eht htiw Em Pleh 模拟 难度:0

    Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2806   Accepted:  ...

  8. New Concept English Two 25 67

    $课文65  小象对警察 683. Last Christmas, the circus owner, Jimmy Gates, decided to take some presents to a ...

  9. c# sqlbulkcopy批量插入数据

    dt信息中包含数据和表名 public static void SqlBulkInsert(DataTable dt, string connStr) { try { using (var conn ...

  10. 常用Mysql存储引擎--InnoDB和MyISAM简单总结

    常用Mysql存储引擎--InnoDB和MyISAM简单总结 2013-04-19 10:21:52|  分类: CCST|举报|字号 订阅     MySQL服务器采用了模块化风格,各部分之间保持相 ...