【问题标题】:How to verify if a dropdown has already pre-populated with a value in selenium如何验证下拉菜单是否已经预先填充了硒中的值
【发布时间】:2019-03-03 06:48:58
【问题描述】:

我需要使用 JAVA 验证下拉菜单是否预填充了文本“全部”,该文本出现在 selenium 的下拉列表中。有人可以帮忙吗? 下面是 HTML 代码。

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

【问题讨论】:

  • 请参阅:How do I do X? SO 的期望是,提出问题的用户不仅会进行研究以回答他们自己的问题,还会分享研究、代码尝试和结果。这表明您已经花时间尝试帮助自己,它使我们免于重复明显的答案,最重要的是它可以帮助您获得更具体和相关的答案!另见:How to Ask

标签: java selenium selenium-webdriver


【解决方案1】:

试试这个。

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

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

检查这个。 Verifying current selection of a dropdown menu

【讨论】:

    【解决方案2】:
    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
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-15
      • 2010-11-30
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2010-10-06
      • 1970-01-01
      相关资源
      最近更新 更多