這是我的 HTML:<tbody> <tr> <td colspan="2" style="text-align:center;font-weight:bold;padding-bottom:10px;">You have Statewide Access<br>Please select the district in which you want to operate</td> </tr> <tr> <td style="text-align:right;width:50%;font-weight:bold;padding-right:10px;"><label for="districtOption">Select District</label></td> <td style="text-align:left;width:50%;"> <select id=`district Option` name="district Option" size="0" alt="Select District" tab index="1"> <option value="00" selected="">-- SELECT --</option> <option value="01"> A1 </option> <option value="02"> A2 </option> </select> </td> </tr>我的第一次嘗試:Select select = new Select(driver.findElement(By.xpath("//select[@id='distrctOption']")));select.selectByVisibleText("A1");第二次嘗試:Select dropdown = new Select(driver.findElement(By.id("districtOption")));dropdown.selectByIndex(01);driver.findElement(By.xpath("//[@value='Select']")).click();控制臺中每次執行一個錯誤消息“線程“main”中的異常org.openqa.selenium.NoSuchElementException Unable to locate element"*** Element info: {Using=xpath, value=//select[@id='districtOption']}我嘗試了所有可能的方法?頁面中沒有框架,但仍然無法選擇它。
2 回答

蕪湖不蕪
TA貢獻1796條經驗 獲得超7個贊
用
Select select = new Select(driver.findElement(By.xpath("//select[@id='district Option']")));
您缺少 xpath 標識中的空格。

森欄
TA貢獻1810條經驗 獲得超5個贊
根據您共享的 HTML 從下拉列表中選擇一個選項,您必須使用<select>該類和相關方法,您可以使用以下任一解決方案:
Select dropdown = new Select(driver.findElement(By.cssSelector("select[name='district Option'][alt='Select District']")));
//Select dropdown = new Select(driver.findElement(By.xpath("//select[@name='district Option' and @alt='Select District']")));
dropdown.selectByValue("01"); //to select the option A1
dropdown.selectByValue("02"); //to select the option A2
添加回答
舉報
0/150
提交
取消