簡體   English   中英

Selenium Java,如何檢查是否選擇了多個單選按鈕?

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

我正在研究一個框架,並嘗試了以下而不是結果:

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()));
    }
}

您好,如果您想驗證是否選擇了單選按鈕,那么請注意,如果選擇了單選按鈕,則其值為= True,如果未選擇,則其所有類型為radio的輸入標簽均具有隱藏的屬性值,即selected值是= null,因此在此基礎上,您可以輕松確定是否選擇了單選按鈕。下面找到相同的工作示例

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();
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM