无法使用selenium Webdriver计算/打印列表框下列出的所有选项

时间:2017-02-15 06:03:53

标签: java eclipse selenium selenium-webdriver

我有这个片段。我试图计算/打印country列表框下列出的所有选项。

  <select id="country" class="text-box drop-down-list ng-pristine ng-not-empty ng-valid ng-valid-required ng-touched" ng-class="{'text-box-error': form.country.$touched && form.country.$invalid}" ng-options="country.name as country.name for country in countries" ng-model="mv.country" name="country" required="" style="">
    <option translate="CUSTOMER_SUPPORT_COUNTRY_SELECT" value="" selected="selected">Select One</option>
    <option label="Afghanistan" value="string:Afghanistan">Afghanistan</option>
    <option label="Aland Islands" value="string:Aland Islands">Aland Islands</option>
    <option label="Albania" value="string:Albania">Albania</option>
    <option label="Algeria" value="string:Algeria">Algeria</option>
    <option label="American Samoa" value="string:American Samoa">American Samoa</option>
    <option label="Andorra" value="string:Andorra">Andorra</option>
    <option label="Angola" value="string:Angola">Angola</option>
    <option label="Anguilla" value="string:Anguilla">Anguilla</option>
    <option label="Antarctica" value="string:Antarctica">Antarctica</option>
    <option label="Antigua and Barbuda" value="string:Antigua and Barbuda">Antigua and Barbuda</option>
    <option label="Argentina" value="string:Argentina">Argentina</option>

我被视为&#39; 1&#39;虽然列表框有很多。

    WebElement X = driver.findElement(By.id("country"));
               Select s=new Select(X);
               List<WebElement> elementCount = s.getOptions();
               int itemSize = elementCount.size();`enter code here`
               System.out.println(itemSize);
                for(int i = 0; i < itemSize ; i++){
                    String optionsValue = elementCount.get(i).getText();
                    System.out.println(optionsValue);

2 个答案:

答案 0 :(得分:0)

谢谢@ guy,你的建议已经解决了我的问题。 我在select语句&amp;之后添加了wait语句-thread.sleep()。它有效。

 WebElement X = driver.findElement(By.id("country"));
       Select s=new Select(X);
       List<WebElement> elementCount = s.getOptions();
       Thread.sleep(2000);
      int itemSize = elementCount.size();
      /*    System.out.println(itemSize);
        for(int i = 0; i < itemSize ; i++){
            String optionsValue = elementCount.get(i).getText();
            System.out.println(optionsValue);

答案 1 :(得分:0)

当我们在select标签中使用ng-options时,需要从java脚本加载选项。因此,Elements将被加载,但选项将花费一些时间从java脚本加载。使用一些等待时间可以避免这种情况。