目前无法识别列表

时间:2017-01-16 06:39:34

标签: java selenium selenium-webdriver

我有一个网页,其中包含一些问题和选项,可以通过单选按钮的形式选择问题。 如下图所示:

enter image description here

我一次只能选择一个单选按钮。 但是我可以在任何时候改变我的选择。

我想在列表中识别当前的Checked / Selected单选按钮。

以下是进行任何选择之前的源页面的html代码

<div class="ec-question-radios ng-scope">
<!-- ngRepeat: option in settings.get('options') -->
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question21" class="ec-question-radios__input ng-pristine **ng-untouched** ng-valid ng-empty" type="radio" value="1" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question21" aria-invalid="false"/>
<span class="ec-question-radios__text ng-binding">                         It wouldn't have an impact on my standard of living and I wouldn't need to use any additional resources (for example, savings) as a result.                     </span>
</label>
</div>
<!-- end ngRepeat: option in settings.get('options') -->
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question22" class="ec-question-radios__input ng-pristine **ng-untouched** ng-valid ng-empty" type="radio" value="2" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question22" aria-invalid="false"/>
<span class="ec-question-radios__text ng-binding">                         I have other resources I could fall back on.                     </span>
</label>
</div>

我找到了一个解决方案,在进行任何选择后,输入标签中的 ng-untouched 类名称将更改为 ng-touching 。 但是,当我将选项从选项1更改为选项2或3或列表中的任何选项时,选项1中的类名仍为 ng-touching 。并且假设我将选项从选项1更改为2,输入标签的类名更改为 ng-touching

如下面的html代码段所示。

<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question21" class="ec-question-radios__input ng-pristine ng-valid ng-not-empty ng-touched" type="radio" value="1" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question21" aria-invalid="false"/>
<span class="ec-question-radios__text ng-binding">                         It wouldn't have an impact on my standard of living and I wouldn't need to use any additional resources (for example, savings) as a result.                     </span>
</label>
</div>
<!-- end ngRepeat: option in settings.get('options') -->
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question22" class="ec-question-radios__input ng-pristine ng-valid ng-not-empty ng-touched" type="radio" value="2" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question22" aria-invalid="false"/>
<span class="ec-question-radios__text ng-binding">                         I have other resources I could fall back on.                     </span>
</label>
</div>
<!-- end ngRepeat: option in settings.get('options') -->
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question23" class="ec-question-radios__input ng-pristine ng-untouched ng-valid ng-not-empty" type="radio" value="3" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question23" aria-invalid="false"/>
<span class="ec-question-radios__text ng-binding">                         It would have an impact on my immediate standard of living.                     </span>
</label>
</div>

我没有采用任何独特的方式来识别当前选择的单选按钮。 任何人都可以请求帮助我吗?

4 个答案:

答案 0 :(得分:1)

您正在寻找的是arg = path & "[" & file & "]Sheet1'!R15C3" 'Acquire the value values(r, 1) = arg 'values(r, 1) = ExecuteExcel4Macro(arg) ,如果当前选中/选中了输入(复选框/单选按钮),则返回isSelected

我不确定此代码是否完全正确,因为我不是True编码器,但它可能看起来像:

Java

答案 1 :(得分:0)

如果您可以为所有单选按钮使用公共属性id或类,则更好。然后首先获得所有单选按钮的列表,然后可以检查选择或取消选择哪一个 让我们假设您的所有单选按钮都有共同的id = question

List<WebElement> allRadiobtn = driver.findElements(By.id("question"));
int iSize = allRadiobtn.size();
boolean bValue = false;
for(int i=0; i < iSize ; i++ ){
   bValue = allRadiobtn.get(i).isSelected();
   if(bValue == true){
           // do some action
        }
}

答案 2 :(得分:0)

<script>
  function add_class(value_check){
  if(value_check == "1"){
    var d = document.getElementById("ec-portfolio-planner-default-questionnaire-question21");
    d.classList.add("ng-untouched");
    document.getElementById("ec-portfolio-planner-default-questionnaire-question22").classList.remove("ng-untouched");
    document.getElementById("ec-portfolio-planner-default-questionnaire-question23").classList.remove("ng-untouched");
    }
    else if(value_check == "2"){
    var d1 = document.getElementById("ec-portfolio-planner-default-questionnaire-question22");
    d1.classList.add("ng-untouched");
    document.getElementById("ec-portfolio-planner-default-questionnaire-question21").classList.remove("ng-untouched");
    document.getElementById("ec-portfolio-planner-default-questionnaire-question23").classList.remove("ng-untouched");
    }
    else if(value_check == "3"){
    var d2 = document.getElementById("ec-portfolio-planner-default-questionnaire-question23");
    d2.classList.add("ng-untouched");
    document.getElementById("ec-portfolio-planner-default-questionnaire-question21").classList.remove("ng-untouched");
    document.getElementById("ec-portfolio-planner-default-questionnaire-question22").classList.remove("ng-untouched");
    }
  }
 </script>
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question21" class="ec-question-radios__input ng-pristine ng-valid ng-not-empty ng-touched" type="radio" value="1" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question" aria-invalid="false" onclick="add_class('1');"/>
<span class="ec-question-radios__text ng-binding">                         It wouldn't have an impact on my standard of living and I wouldn't need to use any additional resources (for example, savings) as a result.                     </span>
</label>
</div>
<!-- end ngRepeat: option in settings.get('options') -->
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question22" class="ec-question-radios__input ng-pristine ng-valid ng-not-empty ng-touched" type="radio" value="2" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question" aria-invalid="false"  onclick="add_class('2');"/>
<span class="ec-question-radios__text ng-binding">                         I have other resources I could fall back on.                     </span>
</label>
</div>
<!-- end ngRepeat: option in settings.get('options') -->
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
<label class="ec-question-radios__label">
<input id="ec-portfolio-planner-default-questionnaire-question23" class="ec-question-radios__input ng-pristine ng-untouched ng-valid ng-not-empty" type="radio" value="3" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question" aria-invalid="false" onclick="add_class('3');"/>
<span class="ec-question-radios__text ng-binding">                         It would have an impact on my immediate standard of living.                     </span>
</label>
</div>

答案 3 :(得分:0)

    <script>
      function add_class(value_check){
      if(value_check == "1"){
        var d = document.getElementById("ec-portfolio-planner-default-questionnaire-question21");
        d.classList.add("ng-untouched");
        document.getElementById("ec-portfolio-planner-default-questionnaire-question22").classList.remove("ng-untouched");
        document.getElementById("ec-portfolio-planner-default-questionnaire-question23").classList.remove("ng-untouched");
        }
        else if(value_check == "2"){
        var d1 = document.getElementById("ec-portfolio-planner-default-questionnaire-question22");
        d1.classList.add("ng-untouched");
        document.getElementById("ec-portfolio-planner-default-questionnaire-question21").classList.remove("ng-untouched");
        document.getElementById("ec-portfolio-planner-default-questionnaire-question23").classList.remove("ng-untouched");
        }
        else if(value_check == "3"){
        var d2 = document.getElementById("ec-portfolio-planner-default-questionnaire-question23");
        d2.classList.add("ng-untouched");
        document.getElementById("ec-portfolio-planner-default-questionnaire-question21").classList.remove("ng-untouched");
        document.getElementById("ec-portfolio-planner-default-questionnaire-question22").classList.remove("ng-untouched");
        }
      }
     </script>

function add_class(value_check){
  if(value_check == "1"){
	var d = document.getElementById("ec-portfolio-planner-default-questionnaire-question21");
	d.classList.add("ng-untouched");
	document.getElementById("ec-portfolio-planner-default-questionnaire-question22").classList.remove("ng-untouched");
	document.getElementById("ec-portfolio-planner-default-questionnaire-question23").classList.remove("ng-untouched");
	}
	else if(value_check == "2"){
	var d1 = document.getElementById("ec-portfolio-planner-default-questionnaire-question22");
	d1.classList.add("ng-untouched");
	document.getElementById("ec-portfolio-planner-default-questionnaire-question21").classList.remove("ng-untouched");
	document.getElementById("ec-portfolio-planner-default-questionnaire-question23").classList.remove("ng-untouched");
	}
	else if(value_check == "3"){
	var d2 = document.getElementById("ec-portfolio-planner-default-questionnaire-question23");
	d2.classList.add("ng-untouched");
	document.getElementById("ec-portfolio-planner-default-questionnaire-question21").classList.remove("ng-untouched");
	document.getElementById("ec-portfolio-planner-default-questionnaire-question22").classList.remove("ng-untouched");
	}
  }
<div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
    <label class="ec-question-radios__label">
    <input id="ec-portfolio-planner-default-questionnaire-question21" class="ec-question-radios__input ng-pristine ng-valid ng-not-empty ng-touched" type="radio" value="1" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question" aria-invalid="false" onclick="add_class('1');"/>
    <span class="ec-question-radios__text ng-binding">                         It wouldn't have an impact on my standard of living and I wouldn't need to use any additional resources (for example, savings) as a result.                     </span>
    </label>
    </div>
    <!-- end ngRepeat: option in settings.get('options') -->
    <div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
    <label class="ec-question-radios__label">
    <input id="ec-portfolio-planner-default-questionnaire-question22" class="ec-question-radios__input ng-pristine ng-valid ng-not-empty ng-touched" type="radio" value="2" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question" aria-invalid="false"  onclick="add_class('2');"/>
    <span class="ec-question-radios__text ng-binding">                         I have other resources I could fall back on.                     </span>
    </label>
    </div>
    <!-- end ngRepeat: option in settings.get('options') -->
    <div class="ec-question-radios__radio ng-scope" data-ng-click="setValue(option.id)" data-ng-repeat="option in settings.get('options')" role="button" tabindex="0">
    <label class="ec-question-radios__label">
    <input id="ec-portfolio-planner-default-questionnaire-question23" class="ec-question-radios__input ng-pristine ng-untouched ng-valid ng-not-empty" type="radio" value="3" data-ng-model="$parent.ngModel" name="ec-portfolio-planner-default-questionnaire-question" aria-invalid="false" onclick="add_class('3');"/>
    <span class="ec-question-radios__text ng-binding">                         It would have an impact on my immediate standard of living.                     </span>
    </label>
    </div>