2016-08-30 33 views
0

我想清除選擇類型的下拉菜單中的數據,但是我不能使用select.clear(),因爲它會返回一個錯誤「元素必須是用戶可編輯的爲了清除它「,這是完全意義上的。你們有沒有知道用什麼命令清除各自的領域?如何用java清除Selenium中的下拉菜單

+1

如果你有一個空白的選項中,選擇。從你的錯誤猜測你有一個普通的選擇下拉列表。 – Grasshopper

回答

1

如果您的元素有<select>標記,則可以使用Select類進行交互。

// first, initialize the WebElement (using the appropriate selector type, this is just an example) 
WebElement element = driver.findElement(By.id("elementId")); 

// next, pass it to the Select object 
Select selectElement = new Select(element); 

// then, select an option from the element 
selectElement.selectByVisibleText("Option Text"); 

,我們還可以通過索引或值(documentation)選擇

// finally, to clear your selection, try: 
selectElement.deselectAll(); 
相關問題