1 回答

TA貢獻2080條經驗 獲得超4個贊
用
driver.findElement(By.className(“ui-datepicker-year”));
而不是
driver.findElements(By.className(“ui-datepick-year”));
當您使用驅動程序.find元素(By.className(“ui-日期選取器-年份”))時,您正在將返回元素存儲在列表中(我預計有 2 個或更多元素與“ui-日期選取器-年份”具有相同的類名)。因此,如果是這種情況,那么您應該注意,硒的構造函數“Select”類將“WebElement”作為參數,它可以是存儲在列表中的第一個或任何元素。那么你的代碼應該寫成這樣-
List<WebElement> NomDOBYear = driver.findElements(By.className("ui-datepicker-
year"));
Select selectYear= new Select(NomDOBYear.get(0));//first element of the list
selectYear.selectByVisibleText("1991");
WebElement NomDOBMonth = driver.findElement(By.className("ui-
datepicker-month"));// you got the class name wrong here
Select selectMonth= new Select(NomDOBMonth);
selectMonth.selectByVisibleText("Nov");
否則,如果整個 DOM 中只有 1 個元素的類名為“ui-日期選取器-年”或“ui-日期拾取器-月”,那么您的代碼應編寫為 -
WebElement NomDOBYear = driver.findElement(By.className("ui-datepicker-year"));
Select selectYear= new Select(NomDOBYear);
selectYear.selectByVisibleText("1991");
WebElement NomDOBMonth = driver.findElement(By.className("ui-datepicker-month"));
Select selectMonth= new Select(NomDOBMonth);
selectMonth.selectByVisibleText("Nov");
我仍然建議使用 XPath 來查找元素。
添加回答
舉報