简体   繁体   中英

Unable to select drop down menu

在此处输入图片说明 I want to automate drop down menu using Selenium web driver using Java, but the HTML page has <option disabledselected>----</option> (Ref to attached screenshot)

I want to select 2nd menu item from drop down. I've tried many things but every time I'm get an error message.

1st Approach - using ByVisibleText :

public void selectHomeCommunity(String name){
    Select hmecomm= new Select(hmecommdropdown);
    hmecomm.selectByVisibleText(name);
}

public <Webelement> SelfRegistrationPage Community(String pass) {
    // TODO Auto-generated method stub
    enterPassKey(pass);
    System.out.println("Entered into Community method");
    pressGoBtn();
}

2nd Approach - JavascriptExecutor :

((JavascriptExecutor)driver).executeScript("document.getElementById('hmecommdropdown').options.item(0).click().;");

3rd Approach - getFirstSelectedOption :

String selectedLabel = new Select(driver.findElement(By.id("CommunityDropdown"))).getFirstSelectedOption().getText();

Every time I'm getting same error as:

waiting for visibility of [[ChromeDriver: chrome on XP (9a6751455dba60b65479430ff8f9aa00)] -> id: CommunityDropdown]

If the dropdowns are from a language such as angular then using Select may not work. My suggestion is us seleniums click operations.

  1. Click the dropdown to open it
  2. Use driver.findElements to find all the webelements for the options inside the dropdown
  3. Iterate through the elements and pull out the text in the elements. When you find the text you are expecting perform the click

Something like the following may be useful:

public void clickDropdownOption(WebDriver driver, String option) throws Exception{
    waitForDropdownMenuToBeVisible(driver);
    WebElement dropdownMenu = driver.findElement(dropdownMenuLocator);
    List<WebElement> optionElements = dropdownMenu.findElements(dropdownOptionLocator);
    for(int i=0; i < optionElements.size(); i++){
        if(optionElements.get(i).getText().equals(option)){
            click(driver, optionElements.get(i));
            waitForDropdownMenuToBeinvisible(driver);
            return;
        }
    }
    throw new Exception("Dropdown option " + option + " was not found");
}

Obviously if it is a normal HTML dropdown then use the conventional approach

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