2016-07-13 1 views
0

divのすべてのlabelアイテムをループする方法私はラジオボタンを持っているinturnのラベルタグの未知数の束があることを意味します。 Selenium WebDriverを使用してください。私は選択したradio buttonを見つける必要があります。二つのものがここにありますのリストで選択したラジオボタンをセレンのwebdriverを使用して検索します

  1. 私は
    <div class="controls"> 
     
    \t <label class="radio inline"> 
     
    \t \t <input type="radio" value="0" name="PlaceOfResidence"/> 
     
    \t Urban   
     
    \t </label> 
     
    \t <label class="radio inline"> 
     
    \t \t <input type="radio" value="1" name="PlaceOfResidence"/> 
     
    \t Suburb   
     
    \t </label> 
     
    \t <label class="radio inline"> 
     
    \t \t <input type="radio" value="2" name="PlaceOfResidence"/> 
     
    \t Rural   
     
    \t </label> 
     
    \t <label class="radio inline"> 
     
    \t \t <input type="radio" value="3" name="PlaceOfResidence"/> 
     
    \t Not Available   
     
    \t </label> 
     
    </div>

    たとえば、選択した無線要素

を見つける必要がありラジオ要素

  • の数を見つける必要がありますここに私が試したことがあります

    private String isRadioButtonSelected2(String name){ 
        List<WebElement> webEl = this.getWrappedDriver().findElements(By.xpath("//input[@type='radio' and @name = '"+name+"']/parent::label")); //and @value='"+value+"']")); 
        String selectedValue = ""; 
        for(WebElement element: webEl){ 
         Boolean selectedRadio = element.isSelected(); 
         if(selectedRadio){ 
          selectedValue =this.getWrappedDriver().findElement(By.xpath("//input[@type='radio' and @name = '"+name+"']/parent::label")).getText(); 
    
          log("&&&&&&&&&&"+selectedValue); 
         }else{ 
          return null; 
         } 
        } 
        return selectedValue; 
    } 
    
  • 答えて

    2

    xpathを使用してすべてのラジオボタンを検索する代わりに、単にBy.nameを使用すると、xpathよりもはるかに高速です。以下のようにしてみてください -

    List<WebElement> radioButtons = this.getWrappedDriver().findElements(By.name("PlaceOfResidence")); 
    int size = radioButtons.size(); 
    // This is the count of total radio button 
    
    for(WebElement radio : radioButtons) 
    { 
        If(radio.isSelected()) 
        { 
        String selectedValue =radio.findElement(By.xpath("./parent::label")).getText(); 
        } 
        } 
    

    はそれが役に立てば幸い... :)働い

    1

    //これは無線素子の数

    List<WebElement> radioButtons = driver.findElements(By.xpath("//input[type=radio]")); int size = = radioButtons.size();

    //上記の要素を反復し、この

    を明確

    ホープ選択された無線要素を識別するisSelected()メソッドを使用する与えます

    +0

    を!私はサイズを繰り返し、isSelected() – shockwave

    関連する問題