给同事写了个demo
public static void main(String[] args) throws Exception {
// 打开首页 进入一个商品目录页 翻转到第二页 将最后一个商品数量选择2 加入购物车 结束
String add1 = "/div[@class=\"itemPrice\"]/div[@class=\"cart_wrapper\"]/span[@class=\"itemLink\"]/button[@class=\"qty_psuh\"]";
String cart1 = "/div[@class=\"itemPrice\"]/div[@class=\"cart_wrapper\"]/a[@class=\"cartimg\"]";
WebDriver dr = new ChromeDriver();
// dr.get("http://www.test.com"); //打开**网
dr.navigate().to("http://www.test.com");
dr.manage().window().maximize(); //最大化窗口
Thread.sleep(2000);
dr.findElement(By.id("lighbox_dist_btn_1")).click(); //点击上海
Thread.sleep(2000);
Actions action = new Actions(dr);
System.out.println("执行鼠标浮动");
action.moveToElement(dr.findElement(By.xpath("//h3[@class=\"catagory\"]/a"))).perform();
System.out.println(dr.findElement(By.xpath("//dt/a")).getText());
Thread.sleep(2000);
dr.findElement(By.xpath("//dt/a")).click();
// 滑动动到最底行
((JavascriptExecutor)dr).executeScript("window.scrollBy(0,document.body.scrollHeight)","");
Thread.sleep(2000);
// 下一页
dr.findElement(By.cssSelector("a[class='page-link next']")).click();
Thread.sleep(2000);
((JavascriptExecutor)dr).executeScript("window.scrollBy(0,document.body.scrollHeight)","");
Thread.sleep(2000);
// 选择最后一个商品
// <div id="shows_201310CM310002630" class="listbox clearfix" is_spec="0" is_color="0" qty="84">
List<WebElement> list = dr.findElements(By.cssSelector("div[class='listbox clearfix']"));
Thread.sleep(2000);
String show = list.get(list.size()-1).getAttribute("id");
// 增加数量
//div[@id=\"shows_201311CM060003806\"]
String add = "//div[@id=\""+show+"\"]"+add1;
dr.findElement(By.xpath(add)).click();
// 加入购物车
// "//div[@id=\"shows_201311CM060003806\"]
String cart = "//div[@id=\""+show+"\"]"+cart1;
dr.findElement(By.xpath(cart)).click();
}