无法从Selenium & java的下拉列表中选择选项

bzzcjhmw  于 2022-12-18  发布在  Java
关注(0)|答案(1)|浏览(205)

我无法单击下拉菜单抛出的特定选项:

客户群具有:

<div class="admin__action-group-wrap admin__action-multiselect-wrap action-select-wrap _active" data-role="wrap-ui-group" tabindex="0" data-bind="
        attr: {
            id: uid,
            name: inputName
        },
        css: {
            _active: listVisible,
            _disabled: disabled,
            _focus: hasFocus,
            _multiple: multiple
        },
        event: {
            keydown: keydownSwitcher,
            focusin: onFocusIn,
            focusout: onFocusOut
        },
        outerClick: outerClick.bind($data)
" id="PR1R77P" name="settings[customer_group_id]">
    <div class="admin__action-multiselect action-select" data-action="open-search" data-bind="click: toggleListVisible">
        <!--ko ifnot: multiple-->
            <div class="admin__action-multiselect-text" data-role="selected-option" data-bind="text: resultLabel">Teleweb</div>
        <!-- /ko-->
        <!--ko if: multiple--><!-- /ko -->
    </div>
    <div class="admin__action-group-list action-select-list action-menu _active" data-bind="css: { _active: listVisible }">
        <div class="admin__action-multiselect-search-wrap">
            <input class="admin__control-text admin__action-multiselect-search" data-bind="
                    hasFocus: searchFocus,
                    event: {
                        keyup: filterOptionsKeydown
                    },
                    valueUpdate: 'afterkeydown',
                    value: filterInputValue" type="text">
            <label class="admin__action-multiselect-search-label" data-action="advanced-select-search" data-bind="attr: {for: uid}, visible: !isSearchActive()
            " for="PR1R77P"></label>
            <label class="admin__action-multiselect-remove-label" data-action="advanced-select-search" data-bind="click: clearSearch, visible: isSearchActive
            " style="display: none;"></label>
            <div class="admin__action-multiselect-search-count" data-bind="visible: quantitySearchItems
                ">
                <!--ko ifnot: multiple-->
                    <span data-bind="text: quantitySearchItems">17</span>
                    <span data-bind="i18n: 'options'">options</span>
                <!-- /ko-->
                <!--ko if: multiple--><!-- /ko-->
            </div>
        </div>
        <!--ko if: group-->
            <!--ko repeat: { foreach: convertedOptions, item: 'optgroup'}--><ul class="admin__action-multiselect-menu-inner _root" data-repeat-index="0">
                <li class="admin__action-group-optgroup">
                    <strong data-bind="text: optgroup().label">Customer Groups</strong>
                </li>
                <!-- ko repeat: { foreach: optgroup().value, item: 'option'}  --><li data-role="option" class="admin__action-group-option" tabindex="-1" data-bind="
                        click: setSelected.bind($context.$data, option().value),
                        event: {
                            hover: resetHover
                        },
                        attr: {
                            'data-value': option().value
                        }" data-repeat-index="0" data-value="11">
                        <div class="action-menu-item _with-checkbox" data-bind="
                                        css: {
                                            _selected: isSelected(option().value)
                                        },
                                        clickBubble: false
                                ">
                            <!--ko if: multiple--><!-- /ko-->
                            <label class="admin__action-multiselect-label">
                                <span data-bind="text: option().label">E.BOSCH</span>
                            </label>
                        </div>
                    </li><li data-role="option" class="admin__action-group-option" tabindex="-1" data-bind="
                        click: setSelected.bind($context.$data, option().value),
                        event: {
                            hover: resetHover
                        },
                        attr: {
                            'data-value': option().value
                        }" data-repeat-index="1" data-value="8">
                        <div class="action-menu-item _with-checkbox" data-bind="
                                        css: {
                                            _selected: isSelected(option().value)
                                        },
                                        clickBubble: false
                                ">
                            <!--ko if: multiple--><!-- /ko-->
                            <label class="admin__action-multiselect-label">
                                <span data-bind="text: option().label">E.BOSCH2</span>
                            </label>
                        </div>
                    </li><!-- /ko -->
            </ul><!--/ko-->
        <!-- /ko-->
        <!--ko ifnot: group--><!-- /ko-->
        <!--ko if: multiple--><!-- /ko -->
    </div>
</div>

我可以通过执行以下操作来单击按钮:

driver.findElement(By.name("settings[customer_group_id]")).click();

我尝试使用以下命令单击特定选项:

driver.findElement(By.xpath("//li[@class='admin__action-group-optgroup' and contains(.,'E.BOSCH')]")).click();

但是它根本不做任何事情。
我做错了什么?为什么我不能点击所需的选项?.
注意:我不能发布该网址,因为它是隐藏的

g2ieeal7

g2ieeal71#

用途

driver.findElement(By.xpath("//span[@data-bind='text: option().label' and text()='E.BOSCH2']")).click();

代替

driver.findElement(By.xpath("//li[@class='admin__action-group-optgroup' and contains(.,'E.BOSCH')]")).click();

问题是xpath不起作用,因为包含文本E.BOSCH的元素不是li,实际上是span

相关问题