Selenium Dropdown选择不起作用

时间:2017-02-21 15:50:03

标签: java selenium select dropdown

通过Eclipse IDE使用Java测试firefox。 以下是我试图测试的网站的HTML代码:

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

默认选项是第1,&#34; ICAO&#34;。 试着让我的测试选择第二,&#34; IATA&#34;通过价值。 Selenium java代码如下:

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

出于某种原因,我无法选择IATA,即使我相信我正在尝试定位它。我的控制台输出如下所示:

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

我可以打印&#34; allSelectedOptions&#34;它显示了&#34;选项值列表&#34;以上。我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

我的代码中存在一些错误,您正在检查if condition中的旧值。您的下拉值中还有空格,因此它与您的string不匹配

更改您的代码,然后尝试 -

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

更新完整代码

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