接上一篇,咱們依然以京東的網站作示例。java
三,單選項網站
下面來作這樣一條case: ui
1. 登陸京東旅行網頁。spa
2. 在國內機票板塊,購買從北京到武漢的往返機票,時間爲明天出發,一週後返回。firefox
3.搜索機票。code
示例代碼:orm
package JD_Practice; import java.text.SimpleDateFormat; import java.util.Calendar; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class SeleniumAction_RadioButton { private static WebDriver driver; private static String baseUrl; public static void main(String[] args) { driver=new FirefoxDriver(); baseUrl="http://jipiao.jd.com/"; driver.get(baseUrl); driver.manage().window().maximize(); PlainTravel_RadioButton(driver); //driver.quit(); } public static void PlainTravel_RadioButton(WebDriver dr){ dr.findElement(By.id("depCity")).clear(); dr.findElement(By.id("depCity")).sendKeys("±±¾©"); dr.findElement(By.id("arrCity")).clear(); dr.findElement(By.id("arrCity")).sendKeys("Î人"); dr.findElement(By.id("roundFlight")).click(); Calendar cal2= Calendar.getInstance(); SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd"); String CurrentDate = f.format(cal2.getTime()); String StartDate = String.valueOf((Integer.valueOf(CurrentDate)+1)); String RoundDate = String.valueOf((Integer.valueOf(CurrentDate)+7)); System.out.println(CurrentDate); System.out.println(StartDate); System.out.println(RoundDate); JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("document.getElementById('depDate').readOnly=false;"); dr.findElement(By.id("depDate")).clear(); dr.findElement(By.id("depDate")).sendKeys(StartDate); js.executeScript("document.getElementById('arrDate').readOnly=false;"); dr.findElement(By.id("arrDate")).clear(); dr.findElement(By.id("arrDate")).sendKeys(RoundDate); dr.findElement(By.id("validQuery")).click(); } }
運行成功後跳轉到機票頁面blog
四,多選項
練習:勾選如下全部複選框ip
@Test public void testUntitled() throws Exception { driver.get(baseUrl + "/Search?keyword=Apple&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&bs=1&wq=Apple&ev=exbrand_Apple%5E&click=6"); driver.findElement(By.xpath("//div[@id='J_feature']/ul/li[1]")).click(); driver.findElement(By.xpath("//div[@id='J_feature']/ul/li[2]")).click(); driver.findElement(By.xpath("//div[@id='J_feature']/ul/li[3]")).click(); driver.findElement(By.xpath("//div[@id='J_feature']/ul/li[4]")).click(); }
其實這個例子寫的並很差,本想着用一個List ,一個Xpath就直接把這四個複選框,所有放到List裏,而後挨個遍歷並勾選。。。But這裏面是有坑的。每次勾選一個過濾條件,頁面就會刷新,接着你List的元素就會找不到 , 須要找到更好的辦法去解決這個問題。utf-8