从Selenium webdriver下拉列表中选择值的问题

时间:2018-01-04 12:59:25

标签: selenium selenium-webdriver

我正在尝试从下拉列表中选择值。以下是登录屏幕上该下拉列表的HTML。

<div class="form-group">
<label for="j_companyname">Company</label>
<select id="j_companyname" name="companyname" size="1" onclick="onAccountLostFocus();" style="cursor: pointer;"><option value="AUTO_DEPLOYER">AUTO_DEPLOYER</option><option value="ES188CLIENT1">ES188CLIENT1</option><option value="ES188CLIENT10">ES188CLIENT10</option><option value="ES188CLIENT11">ES188CLIENT11</option><option value="ES188CLIENT12">ES188CLIENT12</option><option value="ES188CLIENT13">ES188CLIENT13</option><option value="ES188CLIENT14">ES188CLIENT14</option><option value="ES188CLIENT15">ES188CLIENT15</option><option value="ES188CLIENT16">ES188CLIENT16</option><option value="ES188CLIENT17">ES188CLIENT17</option><option value="ES188CLIENT18">ES188CLIENT18</option><option value="ES188CLIENT19">ES188CLIENT19</option><option value="Primatics">Primatics</option></select>
<img src="ui/img/ajax-loader.gif" style="margin: 3px; position: absolute; display: none;">
"

我尝试通过不同的定位器定位元素但无法选择任何值。我的剧本是:

if( driver.findElement(By.id("j_companyname")).isEnabled()){

    System.out.println("Element is Enable");

    WebElement select = driver.findElement(By.id("j_companyname"));
    List<WebElement> options = select.findElements(By.tagName("option"));
    for (WebElement option : options)
    {
        if("ES188CLIENT16".equals(option.getText()))
        //  option.click();
        option.submit();

    }
    }
    else{
        System.out.println("Element is Disabled");
    }

请帮忙。

2 个答案:

答案 0 :(得分:0)

请看下面的代码

XCTAssertFalse

答案 1 :(得分:0)

正如您trying to select the value from the drop down,我们假设我们必须从下拉列表中选择 AUTO_DEPLOYER 。要从下拉列表中选择 AUTO_DEPLOYER ,您可以使用以下代码块:

WebElement my_select = driver.findElement(By.id("j_companyname"));
Select select = new Select(my_select);
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeSelected(By.xpath("//select[@id='j_companyname']//option[contains(.,'AUTO_DEPLOYER')]")));
select.selectByVisibleText("AUTO_DEPLOYER");