我是 Selenium 和 UI 自动化方面的新手。在 span 类中单击动态元素时遇到一些问题。所以这个 id 每次都会为 3 个下拉元素中的每一个更改。因此,每个 this 元素的每个类都相同,这也会产生问题。
所以我需要更改 id="react-select-2585057--value-item"
<div class="field loan-selection">
<label class="field__body">
<div class="field__label">Verwendung
<!-- -->
</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 调用 click()
在动态 id 为 id="react-select-2585057--value-item" 的元素上假设此元素将始终是节点 <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();
关于java - 如何单击 span 类中的动态元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54063348/