简体   繁体   中英

Select an element from a dynamic drop-down

I had the following code for my select:

Select entitySelector = new Select(entitySettingsPage.entitySelector);
entitySelector.selectByVisibleText(entityName);

but now the developer changed the select to button because he had to implement a change. This is how it looks now:

<button _ngcontent-c8="" aria-expanded="true" aria-haspopup="true" class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button" id="entity_settings_glossary_picker_button">
Choose an entity
<span _ngcontent-c8="" class="caret" id="entity_settings_glossary_picker_caret"></span>

how do I work around this change? I also want to mention that the elements from the drop-down have ids like the following: entity_settings_glossary_picker_option_1

If it would have been a string at the end instead of a number, it would have been easy to write a function to identify my element. How do I know what is going to be the number of the element I create? (I always create a new entity for my tests).

Use below code :

WebElement dropdown = driver.findElement(By.id("entity_settings_glossary_picker_button"));
dropdown.click(); // assuming you have to click the "dropdown" to open it
List<WebElement> options = dropdown.findElements(By.tagName("li"));
for (WebElement option : options)
{
    if (option.getText().equals(searchText))
    {
        option.click(); // click the desired option
        break;
    }
}

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