如何在span类中单击动态元素

时间:2019-01-06 16:04:31

标签: java selenium selenium-webdriver xpath webdriverwait

我在Selenium和UI自动化领域还很新。单击span类中的动态元素时遇到一些问题。因此,此ID每次都会针对3个下拉元素中的每个元素进行更改。因此,每个与此元素对应的类都相同,这也会造成问题。

所以我需要更改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>

1 个答案:

答案 0 :(得分:1)

按照 HTML 的要求,在动态ID为 id =“ react-select-2585057--value-item” 的元素上调用click()此元素将始终是节点<div class="field__label">的后代,您需要诱使 WebDriverWait 使所需的元素可点击,并且可以使用以下解决方案:< / p>

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();