简体   繁体   中英

Selenium Java to select from drop down menu

I do appreciate there are variants of this question already posted, however, I have read and tried all options from those posts without success. I think there is something within the HTML in my case which is preventing the solutions from working, specifically the css class ui-helper-hidden-accessible .

The following is my HTML

<div id="myform:selectCharacteristic" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all">
<div class="ui-helper-hidden-accessible">
    <input id="myform:selectCharacteristic_focus" type="text" autocomplete="off" role="combobox" aria-haspopup="true" aria-expanded="false" />
</div>
<div class="ui-helper-hidden-accessible">
    <select id="myform:selectCharacteristic_input" tabindex="-1" data-p-con="javax.faces.Integer" data-p-hl="onemenu">
        <option value="1">Hatchback</option>
        <option value="2">Estate</option>
        <option value="3">Saloon</option>
    </select>
</div>
<label id="myform:selectCharacteristic_label" class="ui-selectonemenu-label ui-inputfield ui-corner-all">&nbsp;</label>
<div class="ui-selectonemenu-trigger ui-state-default ui-corner-right">
    <span class="ui-icon ui-icon-triangle-1-s ui-c"></span>
</div>
<div id="myform:selectCharacteristic_panel" class="ui-selectonemenu-panel ui-widget ui-widget-content ui-corner-all ui-helper-hidden ui-shadow">
    <div class="ui-selectonemenu-items-wrapper" style="height:200px">
        <ul id="myform:selectCharacteristic_items" class="ui-selectonemenu-items ui-selectonemenu-list ui-widget-content ui-widget ui-corner-all ui-helper-reset" role="listbox">
            <li class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" data-label="Hatchback" tabindex="-1" role="option">Hatchback</li>
            <li class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" data-label="Estate" tabindex="-1" role="option">Estate</li>
            <li class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" data-label="Saloon" tabindex="-1" role="option">Saloon</li>
        </ul>
    </div>
</div>

I have tried a variety of things including
org.openqa.selenium.support.ui.Select.selectByIndex() and org.openqa.selenium.support.ui.Select.selectByVisibleText() as trying to perform a click using Actions and then sending Keys.UP and Keys.DOWN . However these options have not been successful.

I would really appreciate a solution which can select an item from the drop down list starting with only label value. That is, if I want to select "Saloon", I do not necessarily know that it has an index value of 3. Also, the drop down can contain many more options which require a scroll on the drop down menu.

Many thanks

https://prnt.sc/k227pf in your case "Select" from selenium is not works, because JSF override base select, you need to write your own methods for select dropdown items. http://prntscr.com/k22c9v it could be something like this:

`

//xpath_for_element_that_can_open_dropdown
 @FindBy(xpath = "//div[contains(@class, 'ui-selectonemenu-trigger')]/span")
 private WebElement SELECT_CORNER;
 //xpath_for_all_elements_indropdownlist
 @FindBy(xpath = "//div[@class='ui-selectonemenu-items-wrapper']//li")
    private List<WebElement> DROPDORN_LIST_ELEMENTS;
    public void selectSomeOption(String dropdownItemToBeSelected){
        SELECT_CORNER.click();
        for (WebElement dropdownListElement : DROPDORN_LIST_ELEMENTS) {
            if (dropdownItemToBeSelected.equals(dropdownListElement.getText())){
                dropdownListElement.click();
                break;
            }
        }
    }

`

As per the HTML you have provided the options Hatchback , Estate and Saloon seems to be <li> items. To select Saloon you can use the following solution:

driver.findElement(By.xpath("//input[@id='myform:selectCharacteristic_focus']")).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='ui-selectonemenu-items ui-selectonemenu-list ui-widget-content ui-widget ui-corner-all ui-helper-reset' and @id='myform:selectCharacteristic_items']//li[@class='ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all' and @data-label='Saloon']"))).click();

I tried this and it's working fine.

System.setProperty("webdriver.chrome.driver", "chromedriver");              
driver = new ChromeDriver();

driver.get("file:///Desktop/test.html");
Thread.sleep(1000);

Select dropdown = new Select(driver.findElement(By.id("myform:selectCharacteristic_input")));
dropdown.selectByVisibleText("Saloon");     
Thread.sleep(1000);  

This code is selecting the Saloon value from the dropdown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM