简体   繁体   中英

Selenium Java, How to check if multiple radio buttons are selected?

I am working on a framework and have tried the following and not results :

List<WebElement> rows = EarningNormal.oRdioList;             
java.util.Iterator<WebElement> i = rows.iterator();         
while(i.hasNext()) {                
    WebElement ContribIDYES = i.next();         
   //System.out.println(sitcodes.getText());                            
    if(ContribIDYES.isSelected()){
       TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES","Contribution ID's are set to YES" , "PASS", "Done");            
    }                   
    else{
       TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES", "Contribution ID's are NOT seto to YES as default", "FAILED",TestDriver.comUtil.getImageFileLoc(TestDriver.globalProps.getWebDriver()));
    }
}

Hi when you want to verify if a radio button is selected or not then please pay attention that any input tag with type radio has a hidden attribute value known as selected if a radio button is selected then its value is = True and if not then its value is = null,hence on the basis of this you can easily identify which radio button is selcted or not .below find a working example for the same

WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("C:\\Users\\rajnish\\Desktop\\myradio.html");

        // working with radio buttons
        // first take all the radio buttons inside a list like below
        List<WebElement> myradioloc = driver.findElements(By.xpath("//*[@type='radio']"));

        // apply the for loop to identify/ verify if a radio button is selected or not
        for(int i=0;i<myradioloc.size();i++){
            System.out.println("attribut value Selected of radio button is : " + myradioloc.get(i).getAttribute("selected"));
            // if radio button is selected then value of selected attribute is True else null
            if( myradioloc.get(i).getAttribute("selected").equals("null")){
                // as if loop will only run when value of selected attribute is null
                // i.e only when  radio button is not selected 
                myradioloc.get(i).click();
            }
        }

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