The general date controls are all pop-up under the Input tab, and if you use Webdriver to set the date, 1. Navigate to the Input2. Use the SendKeys method such as: However, some date controls are readonly, such as 12306.This time, can not call Webelement's SendKeys () scenario One: Use JS remove readonly attribute, then SendKeys or the evil of 12306 for example: Using the departure date, Familiarize the input label readonly with the Javascriptexecutor removeattribute = (javascriptexecutor) driver; Remove readonly attribute Removeattribute.executescript ("var Setdate=document.getelementbyid (\" train_date\ "); Setdate.removeattribute (' readonly '); Scenario Two: Use Click to select the date directly, the date control is an IFRAME, first switch iframe, then find the date you want to set the button click, and then switch out to copy the code webelement dayelement= Driver.findelement (By.xpath ("//span[@id = ' From_imageclick ')");d Ayelement.click ();//Webelement frameelement= Driver.findelement (By.xpath ("//iframe[@border = ' 0 ')");d River.switchto (). FRAME (1);d river.findelement (By.xpath (" tr/td[@onclick = ' Day_click (2015,2,21); '] '). Click (); Driver.switchto (). defaultcontent (); Copy code code as follows: Copy code Webdriver driver=driverfactory.getfirefoxdriver (); Driver.get ("https://kyfw.12306.cn/otn/"); Driver.manage (). window (). Maximize (); Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Driver.manage (). window (). Maximize (); DrIver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Webelement fromstation=driver.findelement (By.xpath ("//input[@id = ' fromstationtext ']"); Fromstation.click (); Fromstation.sendkeys ("Zhengzhou"); Webelement chosefrom =driver.findelement (By.xpath ("//div/span[@class = ' ralign ' and text () = ' Zhengzhou ']")); Chosefrom.click (); Webelement tostation=driver.findelement (By.xpath ("//input[@id = ' tostationtext ']"); Tostation.click (); Tostation.sendkeys ("Shanghai"); Webelement choseelement =driver.findelement (By.xpath ("//div/span[@class = ' ralign ' and text () = ' Shanghai ']"); 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 (); Copy code where: Driverfactory Copy code 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) {S Ystem.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 Operation Calendar Control