-1

I am trying to select the value from the drop down. Below is the HTML of that dropdown on the login screen.

<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;">
"

I tried to locate element via different locators but unable to pick any value. My script is:

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");
    }

Please help.

Ali Azam
  • 2,047
  • 1
  • 16
  • 25
  • Please can you put clear HTML code – Ankur Singh Jan 04 '18 at 13:03
  • Possible duplicate of [How to select an item from a dropdown list using Selenium WebDriver with java?](https://stackoverflow.com/questions/12940592/how-to-select-an-item-from-a-dropdown-list-using-selenium-webdriver-with-java) – JeffC Jan 04 '18 at 14:21

2 Answers2

0

Pleas look below code

       Select drpCountry = new Select(driver.findElement(By.id("j_companyname")));
                drpCountry.selectByVisibleText("ES188CLIENT16");

// these also can help you
                //drpCountry.selectByIndex("givenindex"); 
                //drpCountry.selectByValue("ES188CLIENT16");
Ankur Singh
  • 1,239
  • 1
  • 8
  • 18
0

As you are trying to select the value from the drop down, lets assume we have to select AUTO_DEPLOYER from the dropdown. To select AUTO_DEPLOYER from the dropdown you can use the following code block :

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");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352