一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期,

1. 定位到该input

2. 使用sendKeys 方法

比如:

但是,有的日期控件是readonly的

比如12306的这个

<input id="train_date" class="inp-txt" type="text" value="2015-03-15" name="back_train_date" autocomplete="off" maxlength="10" readonly="readonly" disabled="disabled">

这个时候,没法调用WebElement的sendKeys()

方案一:使用JS remove readonly attribute,然后sendKeys

还是以万恶的12306为例:

使用出发日期,将input标签的readonly熟悉去掉

         JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementById(\"train_date\");setDate.removeAttribute('readonly');") ;

方案二:采用click直接选择日期,日期控件是一个iframe,首先switch iframe,之后找到想要设置的日期button click,然后switch出来

WebElement dayElement=driver.findElement(By.xpath("//span[@id='from_imageClick']"));
dayElement.click();
// WebElement frameElement=driver.findElement(By.xpath("//iframe[@border='0']"));
driver.switchTo().frame(1);
driver.findElement(By.xpath("//tr/td[@onclick='day_Click(2015,2,21);']")).click();
driver.switchTo().defaultContent();

具体代码如下:

WebDriver driver=DriverFactory.getFirefoxDriver();
driver.get("https://kyfw.12306.cn/otn/");
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); WebElement fromStation=driver.findElement(By.xpath("//input[@id='fromStationText']"));
fromStation.click();
fromStation.sendKeys("郑州");
WebElement choseFrom =driver.findElement(By.xpath("//div/span[@class='ralign' and text()='郑州']"));
choseFrom.click(); WebElement toStation=driver.findElement(By.xpath("//input[@id='toStationText']"));
toStation.click();
toStation.sendKeys("上海");
WebElement choseElement =driver.findElement(By.xpath("//div/span[@class='ralign' and text()='上海']"));
choseElement.click();
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementById(\"train_date\");setDate.removeAttribute('readonly');") ;
WebElement setDatElement=driver.findElement(By.xpath("//input[@id='train_date']"));
setDatElement.clear();
setDatElement.sendKeys("2015-02-18");
WebElement dayElement=driver.findElement(By.xpath("//span[@id='from_imageClick']"));
dayElement.click();
// WebElement frameElement=driver.findElement(By.xpath("//iframe[@border='0']"));
driver.switchTo().frame(1);
driver.findElement(By.xpath("//tr/td[@onclick='day_Click(2015,2,21);']")).click();
driver.switchTo().defaultContent();
WebElement searchElement=driver.findElement(By.xpath("//div/a[@id='a_search_ticket']"));
searchElement.click();

其中:DriverFactory

package com.packt.webdriver.chapter3;
import java.io.File;
import java.io.IOException;
import java.util.Arrays; import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.os.WindowsUtils;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities; public class DriverFactory { public static WebDriver getHtmlUnit()
{
HtmlUnitDriver ht=new HtmlUnitDriver();
return ht;
}
public static WebDriver getChromeDriver() { // TODO Auto-generated method stub
String chromdriver="E:\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromdriver);
ChromeDriverService.Builder builder=new ChromeDriverService.Builder();
File file=new File(chromdriver);
// int port=12643;
// ChromeDriverService service=builder.usingDriverExecutable(file).usingPort(port).build();
// try {
// service.start();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(""));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches",
Arrays.asList("--start-maximized"));
options.addArguments("--test-type", "--start-maximized");
WebDriver driver=new ChromeDriver(options);
return driver;
}
public static WebDriver getFirefoxDriver()
{
try
{
WindowsUtils.tryToKillByName("firefox.exe");
}
catch(Exception e)
{
System.out.println("can not find firefox process");
}
File file=new File("d:\\firebug-2.0.4-fx.xpi");
FirefoxProfile profile = new FirefoxProfile();
// profile.setPreference("network.proxy.type", 2);
// profile.setPreference("network.proxy.autoconfig_url", "http://proxy.successfactors.com:8083");
// profile.setPreference("network.proxy.no_proxies_on", "localhost");
// // profile.setPreference("network.proxy.http", "proxy.domain.example.com");
// profile.setPreference("network.proxy.http_port", 8080);
// profile.setPreference("network.proxy.ssl", "proxy.domain.example.com");
// profile.setPreference("network.proxy.ssl_port", 8080);
// profile.setPreference("network.proxy.ftp", "proxy.domain.example.com");
// profile.setPreference("network.proxy.ftp_port", 8080);
// profile.setPreference("network.proxy.socks", "proxy.domain.example.com");
// profile.setPreference("network.proxy.socks_port", 8080); try {
profile.addExtension(file);
profile.setPreference("extensions.firebug.currentVersion", "2.0.4");
profile.setPreference("extensions.firebug.allPagesActivation", "on");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} WebDriver driver = new FirefoxDriver(profile);
return driver; }
public static WebDriver getIEDriver()
{
String IEDriverServer="E:\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver",IEDriverServer);
WebDriver driver=new InternetExplorerDriver();
return driver;
} }

Selenium webdriver 操作日历控件的更多相关文章

  1. Selenium webdriver操作日历控件

    一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如:使用定位: driver.findElement ...

  2. Java+selenium 如何操作日历控件

    场景:一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 但是,有的日期控件是readonly的 ,比如神 ...

  3. selenium操作日历控件

    日历控件是web网站上经常会遇到的一个场景,有些输入框是可以直接输入日期的,有些不能,以我们经常抢票的12306网站为例,详细讲解如何解决日历控件为readonly属性的问题. 基本思路:先用js去掉 ...

  4. selenium webdriver——JS操作日历控件

    一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如 但是,有的日期控件是readonly的 比如12 ...

  5. Selenium webdriver 之select 控件封装,解决onchange问题

    使用webdriver的时候,select 控件经常会绑定onchange 事件,在selenium2.09 之前click 方法对onchange 事件有bug,2.09 以后修复了,但是根据经验也 ...

  6. selenium+Python(Js处理日历控件)

    日历控件是web网站上经常会遇到的一个场景,有些输入框是可以直接输入日期的,有些不能,以我们经常抢票的12306网站为例,详细讲解如何解决日历控件为readonly属性的问题. 基本思路:先用js去掉 ...

  7. webdriver高级应用- 操作日期控件

    1. 通过点击的方式操作日期控件 #encoding=utf-8 from selenium import webdriver import unittest, time, traceback fro ...

  8. selenium - js日历控件处理

    # 13. js处理日历控件 ''' 在web自动化的工程中,日历控制大约分为两种: 1. 可以直接输入日期 2. 通过日历控件选择日期 基本思路: 利用js去掉readonly属性,然后直接输入时间 ...

  9. PyQt5日历控件及相关操作

    1.日历控件QCalendarWidget控件import sys,mathfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQ ...

随机推荐

  1. 我也来写:数据库访问类DBHelper(转)

    一.前言 相信许多人都百度过:“.net 数据库访问类”.然后就出来一大堆SqlHelper.我也用过这些SqlHelper,也自己写过,一堆静态方法,开始使用起来感觉很不错,它们也确实在很多时候可以 ...

  2. 如何在linux环境下安装yaf

    我本机的环境配置 linuxMint17.1 php5.5 nginx1.4.6 下面开始安装 下载最新的yaf包 http://pecl.php.net/package/yaf 我下载的最新版本为2 ...

  3. 一个简单的html5页面在线速成工具!(当然本文主要说下他的成果的结构)

    分享一个好玩的web app页面速成工具 当然主要是让大家看下他的原理 看着他的结构大家就该猜到这个了.这个是利用换页之后给当前div加了一个active,然后利用css控制效果 这个毫无疑问是采用最 ...

  4. [AngularJS] jQuery时代

    抹平浏览器差异的jQuery出现了 jQuery有什么 jQuery使得开发无刷新动态页面(AJAX)或者单页应用(SAP)变得 相当简单. 标准的HTML页面是静态的,被浏览器渲染后就产生了一个DO ...

  5. Request.ServerVariables 参数大全

    Request.ServerVariables("Url") 返回服务器地址 Request.ServerVariables("Path_Info") 客户端提 ...

  6. 使用Ant部署应用程序系统

    1. 首先下载Ant http://ant.apache.org/ 配置环境变量 2. 编写build.xml部署文件如下: <?xml version="1.0" enco ...

  7. 第四章第四个例题(LRJ)

    半年了,最起码的编程能力也谈不上啊,思维神马就更不不敢说了. 互联网时代讲求效率,走得慢和不走没有区别了. The war is on. (buhuidetiduokanduodajibianyehu ...

  8. GZIP压缩

     (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com)         zip压缩文件听说过,GZIP对我可是新鲜词儿,这个世界好复杂,压缩是无处不 ...

  9. [codevs1022]覆盖

    [codevs1022]覆盖 试题描述 有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地.如果要用1×2的矩阵区覆盖(覆盖过程不容许有任何部分重叠)这个陆地,那么最多可以覆盖多少陆地面积. ...

  10. Oracle 10G select工作原理

    数据库查询语句内部执行过程 select * from  table 步骤 分析阶段(parse) 1.共享池库高速缓存有没有该语句.如果有直接返回结果. 2.语法分析sql语句是否正确进行下一步分析 ...