在學習selenium2java的時候,在寫收貨地址相關用例的時候碰到了下拉框,恰好練習了一下select的使用,如今分享,供你們參考。java
//刪除添加收貨地址 public static void deleteAndAddUserAdress(WebDriver driver) throws InterruptedException { clickUser(driver); findElementByTextAndClick(driver, "我的信息"); findElementByTextAndClick(driver, "收貨地址"); clickDeleteAdress(driver); sleep(0); clickSure(driver); AddAddress(driver); String name = getTextByXpath(driver, ".//*[@id='main']/div[2]/div/div/div[1]/p[1]"); assertTrue("添加收穫地址失敗!", name.equals("收貨人:測試收貨人")); }
下面是具體的添加收貨地址的方法:編程
//添加收貨地址 public static void AddAdress(WebDriver driver) { findElementByIdAndClick(driver, "add-address-btn");//點擊添加地址 findElementByXpathAndClearSendkeys(driver, ".//*[@id='LAY_layuipro1a']/div/div[1]/table/tbody/tr[1]/td[2]/div/input", "測試收貨人");//添加收貨人 findElementByXpathAndClearSendkeys(driver, ".//*[@id='LAY_layuipro1a']/div/div[1]/table/tbody/tr[2]/td[2]/div/input", "13120454218");//輸入手機號 //選擇省市縣,以及詳細地址 Select province = new Select(findElementByid(driver, "province-select")); province.selectByIndex(1); Select city = new Select(findElementByid(driver, "city-select")); city.selectByIndex(1); Select area = new Select(findElementByid(driver, "area-select")); area.selectByIndex(1); findElementByClassnameAndClearSendkeys(driver, "textarea", "我是測試地址。"); clickSave(driver); }