Java 当前无法在列表中识别选中/选定的单选按钮
我有一个网页,上面有一些问题和选项,可以选择单选按钮的形式。 如下图所示: 我一次只能选择一个单选按钮。 但是,我可以随时更改选择 我想通过列表中的选中/选中单选按钮来确定当前的位置 下面是进行任何选择之前源页面的html代码Java 当前无法在列表中识别选中/选定的单选按钮,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我有一个网页,上面有一些问题和选项,可以选择单选按钮的形式。 如下图所示: 我一次只能选择一个单选按钮。 但是,我可以随时更改选择 我想通过列表中的选中/选中单选按钮来确定当前的位置 下面是进行任何选择之前源页面的html代码 它不会对我的生活水平产生影响,因此我不需要使用任何额外的资源(例如,储蓄)。 我还有其他可以依靠的资源。 您要查找的是isSelected,如果当前选择/选中了输入(复选框/单选按钮),则返回True 我不确定这段代码是否完全正确,因为我不是Javacoder,但它
它不会对我的生活水平产生影响,因此我不需要使用任何额外的资源(例如,储蓄)。
我还有其他可以依靠的资源。
您要查找的是isSelected
,如果当前选择/选中了输入(复选框/单选按钮),则返回True
我不确定这段代码是否完全正确,因为我不是Java
coder,但它可能看起来像:
for(int i=0; i<=3; i++)
{
if ( driver.findElements(By.xpath("//input[starts-with(@id, 'ec-portfolio-planner-default-questionnaire-question')]")).get(i).isSelected() )
{
WebElement selected_element = driver.findElements(By.xpath("//input[starts-with(@id, 'ec-portfolio-planner-default-questionnaire-question')]")).get(i);
}
}
for(int i=0;i如果可以对所有单选按钮使用公共属性id或类,则效果更好。然后首先获取所有单选按钮的列表,然后可以检查选中或取消选中的单选按钮
让我们假设您的所有单选按钮都具有公共id=问题
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
}
}
List allRadiobtn=driver.findElements(By.id(“问题”);
int iSize=allRadiobtn.size();
布尔bValue=false;
for(int i=0;i
函数添加类(值检查){
如果(值检查=“1”){
var d=document.getElementById(“ec-portfolio-planner-default-Question-question21”);
d、 添加(“ng未触及”);
document.getElementById(“ec-portfolio-planner-default-Questionly-question22”).classList.remove(“ng未触及”);
document.getElementById(“ec-portfolio-planner-default-Question-question23”).classList.remove(“ng未触及”);
}
否则如果(值检查=“2”){
var d1=document.getElementById(“ec-portfolio-planner-default-Questionly-question22”);
d1.类列表。添加(“ng未触及”);
document.getElementById(“ec-portfolio-planner-default-Question-question21”).classList.remove(“ng未触及”);
document.getElementById(“ec-portfolio-planner-default-Question-question23”).classList.remove(“ng未触及”);
}
否则如果(值检查=“3”){
var d2=document.getElementById(“ec-portfolio-planner-default-Question-question23”);
d2.类列表。添加(“ng未触及”);
document.getElementById(“ec-portfolio-planner-default-Question-question21”).classList.remove(“ng未触及”);
document.getElementById(“ec-portfolio-planner-default-Questionly-question22”).classList.remove(“ng未触及”);
}
}
它不会对我的生活水平产生影响,因此我不需要使用任何额外的资源(例如,储蓄)。
我还有其他可以依靠的资源。
这将对我目前的生活水平产生影响。
它不会对我的生活水平产生影响,因此我不需要使用任何额外的资源(例如,储蓄)。
我还有其他可以依靠的资源。
这将对我目前的生活水平产生影响。
谢谢你的回答,我已经尝试了isSelected(),但是它不会在任何时候返回true。即使在迭代该问题的可用答案列表时选择了选项/单选按钮之一。@PriyaP你能检查一下你是否能通过allRadiobtn.get(I)点击该选项。点击()或者尝试使用xpath。如果仍然找不到该选项,则必须尝试其他方法。它允许我单击/选择单选按钮,但当我想要交叉验证时,无法从列表中返回所选单选按钮。我尝试做的是..我正在选择一个选项,移动到另一页,返回到上一页我选择单选按钮选项的ious页面,并验证我选择的单选按钮是否仍处于选中状态。好的。现在我想我必须检查是否为单选按钮设置了任何隐藏值。你能在firebugYea中检查它吗?尝试了许多方法,甚至尝试识别单选按钮CSS属性的任何更改。但还没有成功。也选择了在我的情况下,isChecked不起作用。感谢您的回答,我已经尝试使用isSelected(),但它不会在任何时候返回true。即使在迭代该问题的可用答案列表时选择了选项/单选按钮之一。使用JavaScript找到解决方案。感谢您的所有回答。
<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>
<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>