<div class="Select form-control undefined Select--single is-searchable has-value"><input name="deal.listdealasset[0].acacode" type="hidden" value="NEWCAR"><div class="Select-control"><span class="Select-multi-value-wrapper" id="react-select-3--value"><div class="Select-value"><span class="Select-value-label" id="react-select-3--value-item" role="option" aria-selected="true">New</span></div><div class="Select-input" style="display: inline-block;"><style>input#undefined::-ms-clear {display: none;}</style><input role="combobox" aria-expanded="false" aria-haspopup="false" aria-activedescendant="react-select-3--value" aria-owns="" style="width: 19px; box-sizing: content-box;" value="" data-rxName="deal.listdealasset[0].acacode"><div style="left: 0px; top: 0px; height: 0px; text-transform: none; letter-spacing: normal; overflow: scroll; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-weight: 400; white-space: pre; visibility: hidden; position: absolute;"></div></div></span><span class="Select-arrow-zone"><span class="Select-arrow"></span></span></div></div>
我尝试从下拉列表中选择一个值,但未选择其 html 标签。 我尝试了各种选择,但没有任何效果。
WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'Select-multi-value-wrapper')]"));
List<WebElement> options = optionsList.findElements(By.xpath("//span[contains(@class, 'Select-value-label')]"));
options.get(0).click();
我想从下拉列表中选择新值。[ ]
我在执行时遇到以下错误 - 元素无法滚动到 View 中
下面是该页面的 HTML 代码-
<div class="Select form-control undefined Select--single is-searchable has-value">
<input name="deal.listdealasset[0].acacode" type="hidden" value="NEWCAR">
<div class="Select-control">
<span class="Select-multi-value-wrapper" id="react-select-3--value">
<div class="Select-value"><span class="Select-value-label" id="react-select-3--value-item" role="option" aria-selected="true">New</span></div>
<div class="Select-input" style="display: inline-block;">
<style>input#undefined::-ms-clear {display: none;}</style>
<input role="combobox" aria-expanded="false" aria-haspopup="false" aria-activedescendant="react-select-3--value" aria-owns="" style="width: 19px; box-sizing: content-box;" value="" data-rxName="deal.listdealasset[0].acacode">
<div style="left: 0px; top: 0px; height: 0px; text-transform: none; letter-spacing: normal; overflow: scroll; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-weight: 400; white-space: pre; visibility: hidden; position: absolute;"></div>
</div>
</span>
<span class="Select-arrow-zone"><span class="Select-arrow"></span></span>
</div>
</div>
最佳答案
由于您的下拉菜单由 Div 和 span 标签组成,因此 Select 类将无法在 selenium 中工作。
您可以尝试的代码:
List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
if(element.getText().equals(" your value from drop down")){
element.click();
}
}
如果您还有任何疑问,请告诉我。
关于java - 如何从不是 Selenium 下拉菜单的列表中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56106605/