在学习selenium2java的时候,在写收货地址相关用例的时候碰到了下拉框,刚好练习了一下select的使用,现在分享,供大家参考。

//删除添加收货地址
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);
}