简体   繁体   English

单选按钮的isSelected()问题

[英]isSelected() problems with radio buttons

I have a function whose purpose is to change the state of a radio button, it looks like this: 我有一个函数,其目的是更改单选按钮的状态,如下所示:

public void changeRadioState(List<WebElement> radioButtons){        
    for(WebElement radioButton: radioButtons)
    {
        if (!radioButton.isSelected()){              
            radioButton.click();
        }
    }
}

Now, this works about 90% of the time but sometimes it will click on the non selected button and then go through the loop again and klick back on the previous one, resulting in the state to be the same after the function is run. 现在,这大约有90%的时间有效,但是有时它会单击未选择的按钮,然后再次循环执行,然后快回上一个按钮,从而使功能运行后的状态相同。 witch to me can only mean that the selected state is not found on either of them, altho when I find the elements in the console I find them with a selected state. 对我而言,“巫婆”只能表示未在其中任何一个上找到选定状态,而且当我在控制台中找到元素时,我发现它们处于选定状态。 Anyone know why this would be happening? 有人知道为什么会这样吗?

Just realised since I don't want to change the state more then once I can just use a break in the if. 刚刚意识到,因为我不想再更改状态,所以一旦可以在if中使用中断即可。 but still a wierd problem this function caused. 但此功能仍然引起一个更棘手的问题。

public void changeRadioState(List<WebElement> radioButtons){        
   for(WebElement radioButton: radioButtons)
   {
       if (!radioButton.isSelected()){              
          radioButton.click();
          break;
       }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM