0

I'm new to selenium web driver and I need to select multiple checkbox to Submit the form and the following code response in HTML format.

divs

Please find the attached screenshot and kindly suggest an idea to select multiple checkbox, random etc...

1
  • what is inside div tag? There is only one checkbox could handle action click on your provided snippet. All other inputs are hidden and may throw exception if we perform any actions on them. Commented Aug 7, 2014 at 0:06

5 Answers 5

1

A bit modified @djangofan answer (his code selects not only checkbox inputs):

List<WebElement els = driver.findElements(By.xpath("//input[@type='checkbox']"));
for ( WebElement el : els ) {
    if ( !el.isSelected() ) {
        el.click();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Radek Gredbski, yes it works well now. i used to for loop instead of for each. Can you bit explain were to use the for each and for in Selenium. Thank You.
Another one is if i need to select random checkbox element how to use your code. Can you bit explain me. Thanks
Thats pretty basic thing, see stackoverflow.com/questions/5034370/… Btw there should be no difference between for-each and for loop in that case
I have list of checkbox has selected with Other option, but i want to type in the Other Text box if i choose it. Kindly help me out
1

Its easy. Just do something like this:

List<WebElement els = driver.findElements( By.class( "input") );
for ( WebElement el : els ) {
    if ( !el.isSelected() ) {
        el.click();
    }
}

1 Comment

djangoan, i used as By.class it shows some error when using findElements. I worked out the Radek Grebski comment and it works well. Thanks for your quick answer.
0
@Test(priority=11)
public void Test_CheckBox_Check()throws InterruptedException 
{

    List<WebElement> els = driver.findElements(By.xpath("//div[@class='md-container md-ink-ripple']"));
    System.out.println(Integer.toString(els.size()));

    for ( WebElement el : els ) {
        el.click(); 
    }
}

Comments

0
List<WebElement> 
chk = driver.findElements(By.xpath("//input[@type='checkbox']"));
Iterator<WebElement> itr = chk.iterator(); 
 while (itr.hasNext() ){ 
   if(!itr.next().isSelected())
   itr.next().click();
 }

Comments

0

Check the multiple check boxes using Selenium in python:

checkboxes=browser.find_elements_by_xpath('//input[@type="checkbox"]')

for checkbox in checkboxes:

checkbox.click()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.