使用Selenium Webdriver无法遍历下拉列表

时间:2018-07-23 10:30:40

标签: javascript selenium selenium-webdriver dropdown dropdownbox

enter image description here我正在尝试使用以下代码遍历下拉列表:

new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id
> =\\\"s2id_autogen4_search\\']"))).click();   
    driver.findElement(By.xpath("//*[@id
> =\"s2id_autogen4_search\"]")).click();   
    driver.findElement(By.id("s2id_autogen4_search")).sendKeys("New
> York");  

我要自动执行的下拉列表的HTML是:

<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="select2-results-4" id="s2id_autogen4_search" placeholder="" aria-activedescendant="select2-result-label-68">

详细HTML是:

<ul class="select2-results" role="listbox" id="select2-results-4"><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-22" role="option"><span class="select2-match"></span>Select the state where your business is located</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-23" role="option"><span class="select2-match"></span>Alabama</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-24" role="option"><span class="select2-match"></span>Alaska</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-25" role="option"><span class="select2-match"></span>Arizona</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-26" role="option"><span class="select2-match"></span>Arkansas</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-27" role="option"><span class="select2-match"></span>California</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-28" role="option"><span class="select2-match"></span>Colorado</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-29" role="option"><span class="select2-match"></span>Connecticut</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-30" role="option"><span class="select2-match"></span>Delaware</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-31" role="option"><span class="select2-match"></span>District Of Columbia</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-32" role="option"><span class="select2-match"></span>Florida</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-33" role="option"><span class="select2-match"></span>Georgia</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-34" role="option"><span class="select2-match"></span>Hawaii</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-35" role="option"><span class="select2-match"></span>Idaho</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-36" role="option"><span class="select2-match"></span>Illinois</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-37" role="option"><span class="select2-match"></span>Indiana</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-38" role="option"><span class="select2-match"></span>Iowa</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-39" role="option"><span class="select2-match"></span>Kansas</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-40" role="option"><span class="select2-match"></span>Kentucky</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-41" role="option"><span class="select2-match"></span>Louisiana</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-42" role="option"><span class="select2-match"></span>Maine</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-43" role="option"><span class="select2-match"></span>Maryland</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-44" role="option"><span class="select2-match"></span>Massachusetts</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-45" role="option"><span class="select2-match"></span>Michigan</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted" role="presentation"><div class="select2-result-label" id="select2-result-label-46" role="option"><span class="select2-match"></span>Minnesota</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-47" role="option"><span class="select2-match"></span>Mississippi</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-48" role="option"><span class="select2-match"></span>Missouri</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-49" role="option"><span class="select2-match"></span>Montana</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-50" role="option"><span class="select2-match"></span>Nebraska</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-51" role="option"><span class="select2-match"></span>Nevada</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-52" role="option"><span class="select2-match"></span>New Hampshire</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-53" role="option"><span class="select2-match"></span>New Jersey</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-54" role="option"><span class="select2-match"></span>New Mexico</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-55" role="option"><span class="select2-match"></span>New York</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-56" role="option"><span class="select2-match"></span>North Carolina</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-57" role="option"><span class="select2-match"></span>North Dakota</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-58" role="option"><span class="select2-match"></span>Ohio</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-59" role="option"><span class="select2-match"></span>Oklahoma</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-60" role="option"><span class="select2-match"></span>Oregon</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-61" role="option"><span class="select2-match"></span>Pennsylvania</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-62" role="option"><span class="select2-match"></span>Rhode Island</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-63" role="option"><span class="select2-match"></span>South Carolina</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-64" role="option"><span class="select2-match"></span>South Dakota</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-65" role="option"><span class="select2-match"></span>Tennessee</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-66" role="option"><span class="select2-match"></span>Texas</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-67" role="option"><span class="select2-match"></span>Utah</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-68" role="option"><span class="select2-match"></span>Vermont</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-69" role="option"><span class="select2-match"></span>Virginia</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-70" role="option"><span class="select2-match"></span>Washington</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-71" role="option"><span class="select2-match"></span>West Virginia</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-72" role="option"><span class="select2-match"></span>Wisconsin</div></li><li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation"><div class="select2-result-label" id="select2-result-label-73" role="option"><span class="select2-match"></span>Wyoming</div></li></ul>

1 个答案:

答案 0 :(得分:1)

正如您所提到的,我们可以键入一些要下拉的内容,在这种情况下,您可以尝试类似的操作:

代码:

let instance = this.$.myPara;
instance.appendChild('your markup');

UPDATE1:

如果您先点击下拉上,此代码将起作用。

请注意,您将必须单击下拉列表,然后再使用此代码

WebElement drop_down = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id*='id_autogen'][type='text'][role='combobox']")));  
drop_down.click();
drop_down.sendKeys("New York");  

屏幕截图 enter image description here