>我正在编写一个测试,需要从 select2 下拉列表中选择数据。 Ajax 调用正在获取可以根据场景更改的数据。
如果我输入硬编码值,我的解决方案有效,但该解决方案对我来说不可行
driver.findElement(By.id("select2-company-container")).click();
WebElement dropdown=driver.findElement(By.xpath("//ul[@id='select2-company-results']"));
driver.findElement(By.className("select2-search__field")).sendKeys("Einstein"+ Keys.ENTER);
我已经尝试了下面的代码和其他一些代码,但无法让它运行
driver.findElement(By.id("select2-company-container")).click(); //click on dropdown
driver.findElement(By.className("select2-results__option l1 non-leaf opened showme select2-results__option--highlighted")).click(); //select a value from drop down
错误消息无法使用类找到元素 名称=选择2-results__option L1 非叶开显示 选择2-results__option-突出显示 有关此错误的文档, 请访问: https://www.seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本: '3.141.59', 修订: 'E82be7D358', 时间: '2018-11-14T08:17:03' 系统信息: 主机: "NX00528", IP: '10.28.84.135', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.
<span class="select2-container select2-container--default select2-container--open s2-to-tree" style="position: absolute; top: 97px; left: 302px;"><span class="select2-dropdown select2-dropdown--above" dir="ltr" style="width: 383px;"><span class="select2-search select2-search--dropdown"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox"></span><span class="select2-results"><ul class="select2-results__options" role="tree" id="select2-company-results" aria-expanded="true" aria-hidden="false"><li class="select2-results__option l1" id="select2-company-result-bjvi-96" role="treeitem" aria-selected="false" data-val="96"><span class="item-label">778678676
</span></li><li class="select2-results__option l1" id="select2-company-result-t7b9-89" role="treeitem" aria-selected="false" data-val="89"><span class="item-label">ABC Test
</span></li><li class="select2-results__option l1 non-leaf opened showme select2-results__option--highlighted" id="select2-company-result-rt5a-90" role="treeitem" aria-selected="false" data-val="90"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">ABC Test 1
</span></li><li class="select2-results__option l2 showme" id="select2-company-result-4bcl-91" role="treeitem" aria-selected="false" data-val="91" data-pup="90"><span class="item-label">ABC Test 1-1
</span></li><li class="select2-results__option l1 non-leaf opened showme" id="select2-company-result-xelm-1" role="treeitem" aria-selected="false" data-val="1"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">Einstein
</span></li><li class="select2-results__option l2 non-leaf showme opened" id="select2-company-result-5820-16" role="treeitem" aria-selected="false" data-val="16" data-pup="1"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">E 1
</span></li><li class="select2-results__option l3 non-leaf showme opened" id="select2-company-result-2ek9-60" role="treeitem" aria-selected="false" data-val="60" data-pup="16"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">E 1-1
</span></li><li class="select2-results__option l4 non-leaf showme opened" id="select2-company-result-2bvx-61" role="treeitem" aria-selected="false" data-val="61" data-pup="60"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">E 1-1-1
</span></li><li class="select2-results__option l5 non-leaf showme opened" id="select2-company-result-4ol9-65" role="treeitem" aria-selected="false" data-val="65" data-pup="61"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">E 1-1-1-1
</span></li><li class="select2-results__option l6 showme" id="select2-company-result-gr72-66" role="treeitem" aria-selected="false" data-val="66" data-pup="65"><span class="item-label">E 1-1-1-1-1
</span></li><li class="select2-results__option l3 showme" id="select2-company-result-95uo-62" role="treeitem" aria-selected="false" data-val="62" data-pup="16"><span class="item-label">E 1-1A
</span></li><li class="select2-results__option l2 showme" id="select2-company-result-nfvb-63" role="treeitem" aria-selected="false" data-val="63" data-pup="1"><span class="item-label">E 1A
</span></li><li class="select2-results__option l1 " id="select2-company-result-o60x-79" role="treeitem" aria-selected="false" data-val="79"><span class="item-label">For ClientShop
</span></li><li class="select2-results__option l1 " id="select2-company-result-nq81-58" role="treeitem" aria-selected="false" data-val="58"><span class="item-label">New Company Test
</span></li><li class="select2-results__option l1 " id="select2-company-result-x7t0-80" role="treeitem" aria-selected="false" data-val="80"><span class="item-label">test 2019
</span></li><li class="select2-results__option l1 " id="select2-company-result-79pj-97" role="treeitem" aria-selected="false" data-val="97"><span class="item-label">Test co a1
</span></li><li class="select2-results__option l1 " id="select2-company-result-6j6i-5" role="treeitem" aria-selected="false" data-val="5"><span class="item-label">Test Company (R1)
</span></li><li class="select2-results__option l1 non-leaf opened showme" id="select2-company-result-cfqy-94" role="treeitem" aria-selected="false" data-val="94"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">XYZ 1
</span></li><li class="select2-results__option l2 showme" id="select2-company-result-wpsg-95" role="treeitem" aria-selected="false" data-val="95" data-pup="94"><span class="item-label">XYZ 1-1-1
</span></li><li class="select2-results__option l1 non-leaf opened showme" id="select2-company-result-cx7u-92" role="treeitem" aria-selected="false" data-val="92"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">XYZ Test 2
</span></li><li class="select2-results__option l2 showme" id="select2-company-result-2rso-93" role="treeitem" aria-selected="false" data-val="93" data-pup="92"><span class="item-label">XYZ Test 1-1
</span></li><li class="select2-results__option l1 non-leaf opened showme" id="select2-company-result-ca5f-57" role="treeitem" aria-selected="false" data-val="57"><span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span><span class="item-label">Yum Brands
</span></li><li class="select2-results__option l2 showme" id="select2-company-result-owvf-56" role="treeitem" aria-selected="false" data-val="56" data-pup="57"><span class="item-label">In N Out
</span></li><li class="select2-results__option l1 " id="select2-company-result-uq8f-6" role="treeitem" aria-selected="false" data-val="6"><span class="item-label">Z.Abdul (Customer)
</span></li></ul></span></span></span>
在单击之前使用WebDriverWait
等待元素可单击:
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
//...
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".select2-results__option.l1.non-leaf.opened"))).click();
下面的代码等待所有元素的可见性与.select2-results__option.l1.non-leaf.opened
.然后按期望值"爱因斯坦"过滤可见的并单击。
String value = "Einstein";
driver.findElement(By.className("select2-search__field")).sendKeys(value, Keys.ENTER);
List<WebElement> options = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector(".select2-results__option")))
.stream().filter(element -> element.getText().equals(value)).collect(Collectors.toList());
Assert.assertNotEquals(options.size(), 0, String.format("option with %s text", value));
options.get(0).click();