sabarisan
sabarisan

Reputation: 31

How to select multiple checkbox using selenium webdriver?

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

Upvotes: 0

Views: 36795

Answers (5)

Check the multiple check boxes using Selenium in python:

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

for checkbox in checkboxes:

checkbox.click()

Upvotes: 0

Nains Jain
Nains Jain

Reputation: 1

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

Upvotes: 0

Bhupendra Singh Rautela
Bhupendra Singh Rautela

Reputation: 3596

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

Upvotes: 0

rgrebski
rgrebski

Reputation: 2584

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

Upvotes: 1

djangofan
djangofan

Reputation: 29669

Its easy. Just do something like this:

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

Upvotes: 1

Related Questions