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...

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
sabarisan
  • 31
  • 2
  • 2
  • 9
  • 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. – Nguyen Vu Hoang Aug 07 '14 at 00:06

5 Answers5

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();
    }
}
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 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. – sabarisan Aug 07 '14 at 10:38
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();
    }
}
rgrebski
  • 2,354
  • 20
  • 29
  • 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. – sabarisan Aug 07 '14 at 10:39
  • Another one is if i need to select random checkbox element how to use your code. Can you bit explain me. Thanks – sabarisan Aug 07 '14 at 10:41
  • Thats pretty basic thing, see http://stackoverflow.com/questions/5034370/retrieving-a-random-item-from-arraylist Btw there should be no difference between for-each and for loop in that case – rgrebski Aug 07 '14 at 10:55
  • 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 – sabarisan Aug 07 '14 at 13:17
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(); 
    }
}
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Bhupendra Singh Rautela
  • 3,406
  • 6
  • 21
  • 25
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();
 }
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
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()