-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
  • 40,130
  • 10
  • 33
  • 74
Mansi
  • 63
  • 2
  • 11
  • See: [How do I do X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do-x) The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – JeffC Sep 27 '18 at 14:27

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,021
  • 2
  • 17
  • 20
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
  • 427
  • 4
  • 14