无法从下拉菜单中选择值

时间:2019-07-31 06:25:43

标签: eclipse selenium drop-down-menu automated-tests

我为所选国家/地区提供下拉菜单。当我单击该下拉菜单时,将打开一个搜索框和国家/地区列表。我们可以通过在搜索框中搜索关键字来选择一个国家。但是我无法选择国家“印度”。

WebElement ctry = driver.findElement(By.id("select2-billing_country-container"));
ctry.click();
System.out.println("click on country successfully");
//Error starts from here..
Select country = new Select(ctry);
country.selectByVisibleText("India");

// Html code
// ctry Textbox 
<span class="select2-selection__rendered" id="select2-billing_country-container" role="textbox" aria-readonly="true" title="India">India</span>

// Search box
<input class="select2-search__field" type="text" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="combobox" aria-autocomplete="list" aria-expanded="true" aria-owns="select2-billing_country-results" aria-activedescendant="select2-billing_country-result-k084-IS">

// Specific country india code
<li class="select2-results__option select2-results__option--highlighted" id="select2-billing_country-result-gulv-IN" role="option" data-selected="true" tabindex="-1" aria-selected="true">India</li>

org.openqa.selenium.support.ui.UnexpectedTagNameException:Element should have been "select" but was "span"

1 个答案:

答案 0 :(得分:0)

由于下拉列表不是Select类型,因此不能在此下拉列表上使用Select方法。您需要使用其xpath单击该元素。
您可以这样做:

WebElement ctry = driver.findElement(By.id("select2-billing_country-container"));
ctry.click();
System.out.println("click on country successfully");
WebElement country = driver.findElement(By.xpath("//li[text()='India']"));
country.click();