[英]C# Selenium - How to find out if a radio button is selected
I'm trying to see if a radio button is selected for a test in Selenium.我正在尝试查看是否为 Selenium 中的测试选择了单选按钮。
I can click on the button no problem, but I can't read if it has been selected or not.我可以点击按钮没问题,但我无法阅读它是否被选中。 The HTML looks like this:
HTML 看起来像这样:
I have tried rdioBtn.selected but it always comes back as false.我试过 rdioBtn.selected 但它总是返回错误。
I can't see a checked element.我看不到选中的元素。
How can I tell if it is selected or not?如何判断它是否被选中?
Thanks谢谢
You can try by using javascript executor like below.您可以尝试使用 javascript 执行器,如下所示。
IJavaScriptExecutor executor = (IJavaScriptExecutor)Constants.WebDriver;
bool isChecked=(bool)executor.ExecuteScript("return document.getElementsByName('communication-pref')[0].checked;");
change the "[0]" with index of whichever radio button you are trying to test.使用您尝试测试的任何单选按钮的索引更改“[0]”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.