0

我已經在這個很長一段時間超過2個月。它是從Selenium的下拉框中選擇的。每次我嘗試運行腳本時,都會報告它找不到該元素,可能是什麼問題。 這是我的代碼:硒webdriver從下拉列表中選擇

driver.FindElement(By.CssSelector(UserGroupsConstants.UserGroupType)).Click(); 
driver.FindElement(By.CssSelector(UserGroupsConstants.UserGroupTypeSearch)).SendKeys("User Role"); 

這是我試過,但沒有工作,以及

SelectElement se = new SelectElement(driver.FindElement(By.CssSelector(UserGroupsConstants.UserGroupType))); 
se.SelectByText("User Role"); 

這是下拉 enter image description here enter image description here

+0

也許它被視爲一種價值。嘗試安裝螢火蟲,看看「用戶角色」是否被視爲一個值或文本。或者你可以嘗試:select se = new Select(driver.findElement(By.id(「yourIdHere」))); se.selectByVisibleText(「用戶角色」);並確保您導入了「選擇」庫。還有一件事。如果它是一個值,你只需改變selectByValue(「用戶角色」) –

回答

1

的圖像在第二碼查看頁面源代碼檢查在選擇標籤中定義的下拉字段或按鈕標籤。如果在一個<select>標籤定義下拉下面的腳本應該工作:

final Select droplist = new Select(driver.findElement(By.xpath("xpath-expression of dropdown selection"))); 
      droplist.selectByValue("User Role");    

如果下拉被定義<button>標籤作爲一個先進的選擇,下面的代碼可以幫助你。

driver.findElement(By.xpath("dropdown button xpath-expression")).click(); 
    WebElement ele = driver.findElement(By.xpath("xpath-expression of userrole")); 
    Actions action = new Actions(driver); 
    action.moveToElement(ele).click().build().perform(); 
+0

這顯然不是一個標準的選擇標籤。您只能使用'Select'類作爲'