转载:http://www.oschina.net/code/snippet_273576_15609
首先我先说明一下 我这个用的是Selenium(先说下原理 免得大家不懂什么意思 懂得就飞过吧!有的词是copy的 其他人的说法的!)
Selenium RC主要由两部分组成
第一个是Selenium Server
第二个是Client Libraries
Selenium Server 是用于控制浏览器行为,总的来说,Selenium Server主要包括3个部分: Launcher,Http Proxy,Selenium Core。其中Selenium Core是被Selenium Server嵌入到浏览器页面中的。其实Selenium Core就是一堆JS函数的集合,就是通过这些JS函数,我们才可以实现用程序对浏览器进行操作。
Client Libraries是用来写测试案例时用来控制Selenium Server的库
如要更具体的请观看官网api吧……!
package com.yao; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; public class XiaoMiAutoMain { public static void main(String[] args) { String baseUrl = "http://www.xiaomi.com/"; WebDriver driver = new InternetExplorerDriver(); WebElement element = null; while(true){ try { driver.get(baseUrl); // System.out.println(driver.getPageSource()); //Thread.sleep(30 * 1000); element = driver.findElement(By.className("btnorder")); element.click(); if(driver.getCurrentUrl()!="http://t.hd.xiaomi.com/index.php?_a=20121119_m22_m1s7&_op=choose"){ break; } } catch (Exception ex) { ex.printStackTrace(); try { Thread.sleep(2 * 60 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
element = driver.findElement(By.className("btnorder")); 其中 btnorder 是预定按钮名称
http://t.hd.xiaomi.com/index.php?_a=20121119_m22_m1s7&_op=choose--> 预定网址
查看页面源代码后会看到以下内容
以上两处根据实际情况进行修改(即此应用可以抢购任何东东),在eclipse中直接运行以上代码即可进行自动抢购。
然后需要一个jar包selenium-server-standalone-2.12
.
0
.jar
这个是服务器 cmd进入直接java -jar命令启动即可
java -jar 路劲\selenium-server-standalone-2.12.0.jar
祝大家抢购愉快!!!