当前位置: 首页 > news >正文

Selenium webdriver 操作日历控件

一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如: 但是,有的日期控件是readonly的 比如12306的这个 这个时候,没法调用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[@οnclick='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[@οnclick='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; } }

相关文章:

  • Qt一步一步实现插件调用(附源码)
  • 如何提高Linux下块设备IO的整体性能?
  • 演示:取证分析IPV6组播地址与MAC地址的映射关系
  • 类的运用(一)
  • ODI中通过配置表和自定义逆向工程获取数据库信息
  • vim的Tab键
  • js prototype之诡异
  • 仿博彩业的转盘功能演示
  • 混合敏捷研发(一)SpecDD:混合的敏捷方法
  • 如何在Android模拟器上安装apk文件
  • 【转】iOS 宏(define)与常量(const)的正确使用-- 不错
  • 程序自动生成Dump文件
  • Python:urllib 和urllib2之间的区别
  • 华为实习日记——第十二天
  • Js 读写cookies
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • conda常用的命令
  • es6要点
  • Fastjson的基本使用方法大全
  • jquery ajax学习笔记
  • jQuery(一)
  • python docx文档转html页面
  • TypeScript迭代器
  • Vue官网教程学习过程中值得记录的一些事情
  • 从零搭建Koa2 Server
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 前嗅ForeSpider教程:创建模板
  • 首页查询功能的一次实现过程
  • 小而合理的前端理论:rscss和rsjs
  • 再次简单明了总结flex布局,一看就懂...
  • 正则与JS中的正则
  • 转载:[译] 内容加速黑科技趣谈
  • elasticsearch-head插件安装
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • #define、const、typedef的差别
  • #includecmath
  • #pragma multi_compile #pragma shader_feature
  • (6)设计一个TimeMap
  • (7)STL算法之交换赋值
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (转)Sublime Text3配置Lua运行环境
  • (转)德国人的记事本
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .NET CLR Hosting 简介
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET 常见的偏门问题
  • .NET 动态调用WebService + WSE + UsernameToken
  • .NET文档生成工具ADB使用图文教程
  • .NET中winform传递参数至Url并获得返回值或文件
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • ;号自动换行
  • ?php echo ?,?php echo Hello world!;?
  • [ 第一章] JavaScript 简史