Selenium Java日期选择器点击下一页仅一次生效报element not interactable异常求解
Selenium Java日期选择器点击下一页仅生效一次问题解决方案
根因分析
- 循环逻辑错误:切换月份、年份的两个while循环末尾都加了
break语句,导致循环仅执行1次就强制退出,无法连续切换多个月份/年份 - 点击目标错误:直接点击下一页按钮的子元素
span,实际可交互的是外层的<a>标签,容易触发交互失效 - 缺少显式等待:每次点击下一页后日期选择器DOM会重新渲染,未等待元素就绪就执行下一步,导致元素还未加载完成就触发点击操作
- 逻辑顺序错误:先切换月份再切换年份,逻辑倒置,应该优先匹配年份再匹配月份,避免跨年度切换时逻辑错误
- 陈旧元素问题:点击下一页后原DOM节点失效,未重新定位元素直接操作会触发交互异常
修复要点
- 移除while循环里的
break语句,保证循环能连续执行直到匹配到目标年月 - 点击目标改为下一页按钮的外层
<a>标签,提高交互稳定性 - 每次点击下一页前添加显式等待,保证元素处于可点击状态
- 调整逻辑顺序,先匹配年份再匹配月份
- 每次获取月份、年份前重新定位对应元素,避免陈旧元素异常
修正后核心代码
public boolean dateCheck(String doctorName,String dateAppointment,String Time,String symptom) throws Exception { driver.findElement(By.xpath("//span[contains(text(),'Schedule Appointment')]")).click(); driver.findElement(By.xpath("//input[@value='Create new appointment']")).click(); driver.findElement(By.xpath("//h4[contains(text(),'"+doctorName+"')]/ancestor::ul/" + "following-sibling::button[contains(text(),'Book Appointment')]")).click(); WebElement calendarFrame=driver.findElement(By.xpath("//iframe[@id='myframe']")); driver.switchTo().frame(calendarFrame); driver.findElement(By.xpath("//input[@id='datepicker']")).click(); DateTimeFormatter dateformat = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate appointDate = LocalDate.parse(dateAppointment,dateformat); int appoint_dt = appointDate.getDayOfMonth(); int appoint_mth = appointDate.getMonthValue(); int appoint_yr = appointDate.getYear(); Month targetMonth = Month.of(appoint_mth); WebDriverWait wait = new WebDriverWait(driver, 20); // 优先匹配年份 while(true){ String currentYear = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='ui-datepicker-year']"))).getText(); if(Integer.parseInt(currentYear) == appoint_yr){ break; } // 点击下一页外层可交互a标签 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[./span[@class='ui-icon ui-icon-circle-triangle-e']]"))).click(); } // 再匹配月份 while(true){ String currentMonthStr = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='ui-datepicker-month']"))).getText(); Month currentMonth = Month.valueOf(currentMonthStr.toUpperCase()); if(currentMonth == targetMonth){ break; } wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[./span[@class='ui-icon ui-icon-circle-triangle-e']]"))).click(); } // 选中目标日期 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr/td/a[text()='"+appoint_dt+"']"))).click(); WebElement time_dropDown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@name='time']"))); Select s = new Select(time_dropDown); s.selectByValue(Time); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='ChangeHeatName']"))).click(); WebElement symInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//textarea[@name='sym']"))); if(symptom.isEmpty()) { System.out.println("Please provide your symptoms"); }else{ symInput.sendKeys(symptom); } wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Submit']"))).click(); return true; }
内容的提问来源于stack exchange,提问作者Sri
相关产品推荐
相关产品推荐

