首页 > 代码库 > Slenium_java_4 Try demo on 12306.cn (2)

Slenium_java_4 Try demo on 12306.cn (2)

I was too optimistic.It is take several days working on the demo.. The intresting thing is when I finished the demo, I understand how people make a rob train seat program. JS part still not work, try to figer it out. also, make be I should make a program to rob spring festivel tickets.

sample for change the window in webdriver , but it is not the good way. I should try to find  framename and windowname in js

Set<String> handles = wd.getWindowHandles();
handles.remove(wd.getWindowHandle());//去掉当前句柄
wd.switchTo().window(handles.iterator().next());

The method to change window and frame is below

webdriver.switchTo.window("windowname"); 

webdriver.switchTo.frame("framename");

 

import java.util.Set;import org.openqa.selenium.*;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.interactions.HasInputDevices;public class test12306 {     public static void main(String[] args) throws InterruptedException{ //public static void test()throws InterruptedException{        WebDriver wd = new FirefoxDriver();    //WebDriver wd;    //wd= new FirefoxDriver();    wd.get("http://www.12306.cn/");         //wd.get("www.12306.cn");    Thread.sleep(3000);    wd.findElement(By.cssSelector("img[alt=\"购票\"]")).click();     Thread.sleep(1000);    //change to the webpage frame    Set<String> handles = wd.getWindowHandles();    handles.remove(wd.getWindowHandle());//去掉当前句柄    wd.switchTo().window(handles.iterator().next());        wd.findElement(By.id("login_user")).click();    Thread.sleep(3000);    wd.findElement(By.id("username")).sendKeys("****@**.com");    wd.findElement(By.id("password")).sendKeys("*****");    Thread.sleep(10000);    wd.findElement(By.id("loginSub")).click();        //enter the from station and to station    wd.findElement(By.id("fromStationText")).clear();    wd.findElement(By.id("fromStationText")).sendKeys("上海");    ((HasInputDevices) wd).getKeyboard().sendKeys(Keys.RETURN);        wd.findElement(By.id("toStationText")).clear();    wd.findElement(By.id("toStationText")).sendKeys("杭州");    ((HasInputDevices)wd).getKeyboard().sendKeys(Keys.RETURN);        //enter the data from keyboard using js    String str="document.getElementById(\"startdatepicker\").readonly=false";        //String str="document.getElementById(\"startdatepicker\").readonly=false";     String strDate="document.getElementById(\"Startdatepicker\").value\"2014-01-08\"";    ((JavascriptExecutor)wd).executeScript(str);    ((JavascriptExecutor)wd).executeScript(strDate);    wd.findElement(By.id("a_search_ticket")).click();    wd.quit(); }}

 

Slenium_java_4 Try demo on 12306.cn (2)