Automating Complex Course Creation with Selenium Java: A Full Code Walkthrough
This article provides a detailed Selenium‑Java script for automating the creation of formal and live courses, covering element selection, date‑time handling, file uploads, random live‑room assignment, and crucial conditional checks, complemented by screenshots of the UI dialogs.
The author shares a comprehensive Selenium‑Java automation script used to create both formal and live courses on an education platform, illustrating how to handle a variety of UI interactions such as selecting grades, subjects, teachers, dates, times, and uploading media.
Creating a Formal Course
public static void createFormalCourseAndSale(WebDriver driver, String grade, String subject) throws InterruptedException {
clickCourse(driver);
clickFormalCourse(driver);
clickAddCourse(driver);
findElementByIdAndClearSendkeys(driver, "input-title", "测试班课"+grade+subject); // input course title
findElementByIdAndClick(driver, "button-toggle-grade"); // select grade
findElementByTextAndClick(driver, grade);
findElementByIdAndClick(driver, "button-toggle-subject"); // select subject
findElementByTextAndClick(driver, subject);
findElementByIdAndClearSendkeys(driver, "input-description", "测试招生人数1人"); // input description
findElementByIdAndClick(driver, "button-toggle-teacher_id"); // select main teacher
findElementByTextAndClick(driver, "李");
// ... (additional field inputs omitted for brevity) ...
findElementByIdAndClick(driver, "button-toggle-live_address_id"); // select live room
findElementByXpath(driver, ".//*[@id='dropdown-live_address_id']/li["+getRandomInt(5)+"]/a"); // random live room to avoid conflict
// Save and confirm
clickSave(driver);
boolean key1 = true;
while(key1){
if (exists(driver, By.xpath("html/body/div[12]/h2")) & getTextByXpath(driver, "html/body/div[12]/h2").equals("保存成功")) {
key1 = false;
break;
}
}
clickSure(driver);
}The script uses helper methods like clickCourse, findElementByIdAndClick, and findElementByTextAndClick to interact with the page, and it inserts static values for fields such as price, enrollment limit, and video URLs.
Creating a Live Course
// After confirming the formal course is saved, the script proceeds to create a live session
findElementByIdAndClearSendkeys(driver, "input-title", "直播课1"); // live course name
findElementByIdAndClick(driver, "input-begin_time_date"); // start date
findElementByTextAndClick(driver, "30");
findElementByIdAndClick(driver, "input-begin_time_time"); // start time
// Select specific start and end times via XPath
findElementByXpathAndClick(driver, "html/body/div[3]/div[3]/div/div[2]/div[2]"); // 1:00 start
findElementByXpathAndClick(driver, "html/body/div[3]/div[3]/div/div[3]/div[1]"); // end time selection
// Set deadlines for exercises and revisions, upload cover image, etc.
// Save and verify success using the same boolean‑loop pattern as aboveA critical note highlighted by the author is that the conditional check must use the single ampersand ( &) instead of the logical AND ( &&); using && would cause a runtime error in this context.
Verification Loops
boolean key2 = true;
while(key2){ // wait for save confirmation
if (exists(driver, By.xpath("html/body/div[8]/h2")) & getTextByXpath(driver, "html/body/div[8]/h2").equals("保存成功")) {
key2 = false;
break;
}
}Both loops rely on the presence of a specific heading element and the exact text "保存成功" ("Save Successful") to determine when the operation has completed.
Visual Reference
The article includes screenshots of the modal dialogs encountered during the automation process:
Overall, the guide demonstrates how to script a complex series of UI actions with Selenium, handle dynamic elements, and reliably verify each step’s success.
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.
