selenium webdriver how to select from list menu it

2019-06-26 06:34发布

How do I select an item in the list of menu items once it appears as a dropdown? I tried sendKeys to input the text like "Brown Mustard", but it clears out when I hit the submit button. I know I should be able to type it in the field but WebDriver sendKeys didn't work, so if you have suggestions on how to select from a list menuitems, thanks so much!

Here's the html snippet of the text field and the menu items that appear when you enter 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>

3条回答
成全新的幸福
2楼-- · 2019-06-26 07:10

After entering B, you could create a select object of the 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");
查看更多
放我归山
3楼-- · 2019-06-26 07:14

This is how it shall 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: It shall select the first/top menu item after sending keys. So, type more keys if you wanted to select a particular item.

查看更多
在下西门庆
4楼-- · 2019-06-26 07:32

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...,the 3rd statement will click on the menu item..

查看更多
登录 后发表回答