How do you click on a checkbox from a list of checkboxes via Selenium/Webdriver in Java?

后端 未结 8 2000
北海茫月
北海茫月 2020-12-01 22:59

I\'m using Selenium 2 (Webdriver) for automating tests on a webpage. However I wonder if there is way to check checkbox from the list of checkboxes using webdriver framework

相关标签:
8条回答
  • 2020-12-01 23:45

    Try using this piece of code written in java

    String checkboxes = "//*[@type='checkbox']";
    List<WebElement> elementToClick = driver.findElements(By.xpath(checkboxes));
    for (WebElement AllCheck : elementToClick) {
        AllCheck.click();
    }
    
    0 讨论(0)
  • 2020-12-01 23:49

    You can select each of the radio buttons/checkboxes by selecting the element which contains them and iterating through each one just like an array.

    For example, here a ul element contains some radio buttons. I select the ul element first, then I can select each radio by using the correct index (inside the [])

     //Select the ul containing the radio buttons I want to click/select
    
       var ul = driver.FindElement(By.Id("ul_containing_radio_buttons"));
    
       //use forloop to click each button in turn         
       for (var i = 2; i <= 0; i--)
             {
                var button= ul.FindElements(By.TagName("input"))[i];
                //i is the index of the radio button in out ul element
                button.Click();
             }
    
    0 讨论(0)
提交回复
热议问题