下拉列表的HTML代码,包含3个常量值:
<div class="a-popover-inner" style="height: auto; overflow-y: auto; min-width: 107px; width: auto;">
<ul id="3_dropdown_combobox" class="a-nostyle a-list-link" aria-multiselectable="false" role="listbox" tabindex="-1">
<li class="a-dropdown-item status-option" role="option" tabindex="0">
<li class="a-dropdown-item status-option" role="option" tabindex="0">
<a class="a-dropdown-link a-active" data-value="{"stringVal":"Active"}" href="javascript:void(0)" tabindex="-1"> Active </a>
</li>
<li class="a-dropdown-item status-option" role="option" tabindex="0">
</ul>
尝试:
driver.findElement(By.xpath("//*[@id='status-select']/span/span"));
结果:
Able to click/select the dropdown successfully
但无法进一步选择特定的下拉列表
Try1:
driver.findElement(By.xpath("//*a[@data-value={'stringVal':'Active'}]")).click();
Result1:InvalidSelectorError: Unable to locate an element with the xpath expression
Try2:
java.util.List<WebElement> elements = driver.findElements(By.xpath("//*[@id='status-select']/span/span"));
Result3: count = element.size(); // prints count as 1
//so cant get elements[element.count-1];
Try3:
Select select = new Select(driver.findElement(By.id("3_dropdown_combobox")));
select.selectByVisibleText("Expired");
Result3:
Try4:
driver.findElement(By.xpath("//*[@id='3_dropdown_combobox']/li[2]/a")).click();
(or)
java.util.List<WebElement> elements = driver.findElements(By.id("3_dropdown_combobox"));elements.size();
结果4:控制台对&gt; 10分钟没有任何作用。我停止执行
请指导我选择第二/第三下拉选项的正确方法
答案 0 :(得分:0)
这样做:
Select select = new Select(driver.findElement(By.id("status-select")));
select.selectByIndex(1); //Selects the 2nd option in the dropdown list
答案 1 :(得分:0)
要点击Active
链接,这应该适合您:
driver.findElement(By.xpath("//*[@id="3_dropdown_combobox"]/li[2]/a")).click();
我试过
driver.findElement(By.xpath("//*[@id="3_dropdown_combobox"]/li[2]/a")).getText();
并按预期得到了Active
这个词。
尝试查看链接是否将您带到正确的位置