如何单击 span class 内的动态元素

How to click dynamic element inside span class

我对 Selenium 和 UI 自动化还很陌生。单击 span class 内的动态元素时遇到一些问题。所以这个 id 每次都会为 3 个下拉元素中的每一个更改。所以每个 class 对于每个这个元素都是一样的,这也会产生问题。

所以我需要更改 id="react-select-2585057--value-item"

的值

<div class="field loan-selection">
        <label class="field__body">
            <div class="field__label">Verwendung
                <!-- -->&nbsp;
            </div>
            <div class="field__control">
                <div class="Select customSelect has-value Select--single">
                    <div class="Select-control">
                        <span class="Select-multi-value-wrapper" id="react-select-2585057--value">
                            <div class="Select-value">
                                <span class="Select-value-label" role="option" aria-selected="true" id="react-select-2585057--value-item">Freie Verwendung</span>
                            </div>
                            <div aria-expanded="false" aria-owns="" aria-activedescendant="react-select-2585057--value" aria-disabled="false" class="Select-input" role="combobox" style="border:0;width:1px;display:inline-block" tabindex="0"></div>
                        </span>
                        <span class="Select-arrow-zone">
                            <span class="Select-arrow"></span>
                        </span>
                    </div>
                </div>
            </div>
        </label>
    </div>

根据 HTML 在动态 ID 为 id="react-select-2585057--value-item"[ 的元素上调用 click() =20=] 假设此元素将始终是节点 <div class="field__label"> 的后代,您需要引入 WebDriverWait 以使所需的 元素可点击,您可以使用以下解决方案:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='field loan-selection']//div[@class='field__label' and contains(., 'Verwendung')]//following::div[1]//span[@class='Select-value-label' and starts-with(@id,'react-select-')]"))).click();