Automating Shipping Address Add/Delete with Selenium Java Select
This guide demonstrates how to use Selenium WebDriver with Java to delete an existing shipping address and add a new one, including handling dropdown selections for province, city, and area, with complete code examples and step‑by‑step explanations.
While learning Selenium with Java, the author needed to test shipping‑address functionality that involves a dropdown menu. To practice the Select class, they created reusable methods for deleting an existing address and adding a new one, and share the full implementation for reference.
Method: deleteAndAddUserAdress
// Delete then add shipping address
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("收货人:测试收货人"));
}This method navigates to the user profile, opens the address management page, clicks the delete button, confirms the deletion, then calls the address‑adding routine. Finally it verifies that the newly added address matches the expected text.
Method: AddAdress
// Add shipping address
public static void AddAdress(WebDriver driver) {
findElementByIdAndClick(driver, "add-address-btn"); // Click "Add Address"
findElementByXpathAndClearSendkeys(driver, ".//*[@id='LAY_layuipro1a']/div/div[1]/table/tbody/tr[1]/td[2]/div/input", "测试收货人"); // Enter recipient name
findElementByXpathAndClearSendkeys(driver, ".//*[@id='LAY_layuipro1a']/div/div[1]/table/tbody/tr[2]/td[2]/div/input", "13120454218"); // Enter phone number
// Select province, city, area, and fill detailed address
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);
}The AddAdress method clicks the "Add Address" button, fills in the recipient name and phone number, selects the first option for province, city, and area using Selenium's Select class, enters a sample detailed address, and finally saves the new entry.
Both methods rely on helper functions such as clickUser, findElementByTextAndClick, clickDeleteAdress, clickSure, clickSave, and various findElement… utilities, which abstract common WebDriver actions. The code can be integrated into a larger test suite for end‑to‑end verification of address management features.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
