Selenium webdriver how to choose from a menu list

How to select an item in the list of menu items after its drop-down list? I tried sendKeys to enter text such as "Brown Mustard", but it clears when I click the "Send" button. I know that I would have to enter it in the field, but WebDriver sendKeys did not work, so if you have suggestions on how to select from the list of menu items, thank you very much!

Here, the html fragment of the text field and the menu items that appear as you type say "B"

<input id="combobox0-text" class="ui-autocomplete-input ui-widget ui-widget-content tableRightFormTextField" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true"> <li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">Bro<strong>w</strong>n Mustard</a></li> <li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">Bro<strong>w</strong>ntop</a></li> 
+4
source share
3 answers

you can try using wait,

 new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOfElementLocated(By.id("combobox0-text"))).clear(); driver.findElement(By.id("combobox0-text")).sendKeys("Brown Mustard"); new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li.ui-menu-item"))).click(); 

the above code will clear the input field and type in the required item and wait for the menu item to appear in the drop-down list ..., the 3rd operator will click on the menu item ..

+2
source

Here's how it will work:

 driver.FindElement(By.Id("combobox0-text")).Clear(); driver.FindElement(By.Id("combobox0-text")).SendKeys("bro"); driver.FindElement(By.CssSelector("li.ui-menu-item")).Click(); 

FYI: after sending the keys, he should select the first / top menu item. So enter more keys if you want to select a specific item.

+1
source

After entering B, you can create an object to select menu items, and then select items based on visible text

 import org.openqa.selenium.support.ui.Select; //your code before entering B Select menu = new Select(driver.findElement(By.id("combobox0-text"))); menu.selectByVisibleText("Brown Mustard"); 
+1
source

All Articles