简体   繁体   中英

Get and compare first item from drop down menu with an string in selenium webdriver

I have a dropdown menu with 2 options(option1 and option2).I want to get the NAME of the first option and compare with a string. I tried something like this but not work for me:

The drop down menu has two items:
            -first(default) is "simplex"
            -second is "duplex"

The HTML drop down is:

    <div class="fieldHelp " id="fieldHelp8352">
<script></script>
<select id="8352" class="lazy-droplist" aria-labelledby="8351" name="pMode:value" style="display: none;">

<option selected value="0">simplex</option>

<option value="1">duplex</option>
</select>
<label id="extendClickArea_8352" style="height:40px; width:100%; position: relative;" class="lazy-droplist">
<span class="ui-selectmenu-button lazy-droplist" tabindex="0" id="8352-button" role="combobox" aria-expanded="false" aria-  autocomplete="list" aria-owns="8352-menu" aria-haspopup="true" style="width: 100%;" aria-activedescendant="ui-id-1003" aria-    labelledby="ui-id-1003" aria-disabled="false" data-modalfocus="">
<span class="ui-icon ui-icon-triangle-1-s"></span>
<span class="ui-selectmenu-text">simplex</span>
</span>
</label>
<div id="bs-help-modal-sm-8352" style="display: none;" class="bs-help-modal-sm-8352 ui-draggable" tabindex="-1" role="dialog"   aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-content"><div class="modal-header ui-draggable-handle">
<button type="button" class="close" aria-label="Close" onclick="closeHelpPopup(event, $('#bs-help-modal-sm-8352'));">
<span aria-hidden="true">×</span></button>
.....
.....
</div>
<script>
$('#bs-help-modal-sm-8352').draggable({ handle: '.modal-header' });
</script>
</div>

Noticed that the code is somehow grayout,inactive but when I tried to extract test "simplex" ,not said that the menu is hidden or something...

Java code is:

            Select select = new Select(driver.findElement(By.name("simplex")));
            WebElement option = select.getFirstSelectedOption();
            String defaultItem = option.getText();
            System.out.println(defaultItem);->this variable is empty :(

            if (defaultItem == "simplex") {
                System.out.println("Is Simplex");
            } else {

                System.out.println("Is Duplex");

Where I do something wrong because defaultItem is empty... Thks

In your case, I cant see name attribute with "simplex" anywhere in the code provided by you. Also, a problem I can see in HTML code is it does not have any value for a selected attribute that's why you getting the empty value:

<option selected="" value="0">simplex</option>

Try below method:

Select select = new Select(driver.findElement(By.id("8352")));
List<WebElement> options =  select.getOptions();
int optionssize=    options.size();
for(int i=0;i<optionssize;i++)
{
  if(options.get(i).getText().equals("Simplex"))
  {
    System.out.println("Is Simplex available at"+i);
    break;
  }
}

Solved. The solution was:

String Text = driver.findElement(By.id("the_direct_path_to_this_span_<span class="ui-selectmenu-text">simplex</span>")).getText();
if(text.equals("Simplex"))
{
System.out.println("Is Simplex");
} else {

System.out.println("Is NOT Simplex");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM