java - 如何从不是 Selenium 下拉菜单的列表中获取值

标签 java selenium selenium-webdriver

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

我想从下拉列表中选择新值。[ enter image description here ]

我在执行时遇到以下错误 - 元素无法滚动到 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/

相关文章:

java - JPype编译问题

javascript - 无法在android webview中实现javascript

java - 在反编译的 apk 上恢复启用缩小和 proguard 效果

selenium - 到达错误页面 : about:neterror when trying to navigate to other tabs if there is a form submit under that tab

javascript - send_keys() 的替代方案 - selenium python

java - 从 java“插入”Access 2010

Java Selenium HtmlUnitDriver - 由于 Ajax 调用,Gwt 源未填充

python - 如何等待一个下拉列表处理完毕后再转到下一个下拉列表?

java - 将所有对象放在 xml 文件中的一个位置或创建每个类的最佳方法是什么?

Python + Selenium -- 无法点击元素