-1

I need to verify that a drop-down is prefilled with text "All" which is present in the dropdown list in selenium using JAVA. Can anyone please help? Below is the HTML code.

<div _ngcontent-c3="" class="input-group">
  <span _ngcontent-c3="" class="input-group-addon"><i _ngcontent-c3="" class="fa fa-flash"></i></span>
  <select _ngcontent-c3="" class="form-control m-b ng-untouched ng-pristine ng-valid" id="report-status-id-select" name="reportStatusId">
    <!---->
    <option _ngcontent-c3="" value="0: 0">All</option>
    <option _ngcontent-c3="" value="1: 4">In Action</option>
    <option _ngcontent-c3="" value="2: 5">Completed</option>
    <option _ngcontent-c3="" value="3: 6">Closed</option>
    <option _ngcontent-c3="" value="4: 10">PJP</option>
  </select>                         
</div>
Guy
  • 46,488
  • 10
  • 44
  • 88
Mansi
  • 63
  • 3
  • 11

2 Answers2

0
Select dropdown = new Select(driver.findElement(By.id("id")));

//Get all options
List<WebElement> dd = dropdown.getOptions();

//Get the length
System.out.println(dd.size());

// Loop to print one by one
for (int j = 0; j < dd.size(); j++) {
    if(dd.get(j).getText().equals("All"))
    // TO DO Code

}
Amit Jain
  • 4,389
  • 2
  • 18
  • 21
0

Try this.

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));

WebElement option = select.getFirstSelectedOption().getText( );

Check this. Verifying current selection of a dropdown menu

Sugan
  • 447
  • 1
  • 4
  • 16