I am currently working on automating an application using Selenium Webdriver with Java. The web application I am testing has an Add button that, when clicked, triggers the activation of a dropdown menu. Subsequent clicks on the Add button reveal additional dropdown menus, each identified by IDs page1, page2, page3, and so forth.
My goal is to automatically identify any existing dropdown menus upon opening the page, select the next available one if present, and then choose a value from that dropdown menu.
The following is my current code snippet where I manually handle each dropdown selection:
driver.findElement(By.id("addPage")).click();
new Select(driver.findElement(By.id("page0"))).selectByVisibleText("ABCD");
driver.findElement(By.id("addPage")).click();
Thread.sleep(1000);
new Select(driver.findElement(By.id("page1"))).selectByVisibleText("CDEF");
driver.findElement(By.id("addPage")).click();
Thread.sleep(1000);
new Select(driver.findElement(By.id("page2"))).selectByVisibleText("EFGH");
driver.findElement(By.id("addContact")).click();