简体   繁体   中英

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.

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:

radio button code

I have tried rdioBtn.selected but it always comes back as false.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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