NewProgrammer686
NewProgrammer686

Reputation: 65

Selenium Dropdown selection not working

Testing firefox with Java via Eclipse IDE. Below is the HTML code from the website I am trying to test:

<td class="selectCommandHolder">
<select id="MainContent_DropDownQueryField" name="ctl00$MainContent$DropDownQueryField" aria-invalid="false">
<option value="ICAO" selected="selected"> ICAO </option>
<option value="IATA"> IATA </option>
<option value="Airfields.Name"> Name </option>
<option value="City"> City </option>
<option value="States.Name"> State </option>
</select>
</td>

The default option is the 1st, "ICAO". Trying to have my test select the 2nd, "IATA" via value. Selenium java code is below:

    WebElement searchOption = driver.findElement(By.id("MainContent_DropDownQueryField")); 

    Select searchIATA = new Select(searchOption); 

    WebElement selected_value = searchIATA.getFirstSelectedOption();
    System.out.println("Selected values is "+ selected_value.getText());

    Thread.sleep(1000);
    searchIATA.selectByValue("IATA");

    if (searchIATA.getFirstSelectedOption().getText().trim().equals("IATA")){
        System.out.println("succesfully selected IATA.");
    } else{
        System.out.println("selected value is not IATA, it is:" + selected_value.getText());
    }

For some reason, I am unable to select IATA, even though I believe I am properly trying to target it. My console output looks like this:

1487691940734   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940749   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940760   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940769   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940777   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
Selected values is ICAO
1487691941789   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941795   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941801   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941822   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941829   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
Selected values is not IATA, it is: ICAO

I am able to print "allSelectedOptions" and it shows the list of "option values" above. What am i missing here?

Upvotes: 0

Views: 1479

Answers (1)

NarendraR
NarendraR

Reputation: 7708

I thing there is some mistake in your code, You are checking old value in if condition Also there is white space in your dropdown value so it is not matching with your string

Change your code as bellow and try-

searchIATA.selectByValue("IATA");
if (searchIATA.getFirstSelectedOption().getText().trim().equals("IATA"))
 {
  System.out.println("Succesfully selected IATA.");
 }
  else
  {
      System.out.println("Selected values is not IATA, it is: "+ selected_value.getText());
  }

Updated Complete Code

    WebElement searchOption = driver.findElement(By.id("MainContent_DropDownQueryField")); 

    Select searchIATA = new Select(searchOption); 

    WebElement selected_value = searchIATA.getFirstSelectedOption();
    System.out.println("Selected values is "+ selected_value.getText());

    Thread.sleep(2500);
    searchIATA.selectByValue("IATA");
    Thread.sleep(2500);
    System.out.println(searchIATA.getFirstSelectedOption().getText());

    if (searchIATA.getFirstSelectedOption().getText().trim().equals("IATA"))
    {
        System.out.println("Succesfully selected IATA.");
    }
    else
    {
         System.out.println("Selected values is not IATA, it is: "+ selected_value.getText());
    }
}

Upvotes: 2

Related Questions