Selecting checboxes based on the parameters
<p>Let's say I have bunch of check boxes listed as below:</p>

<ol>
<li>All</li>
<li>Test1</li>
<li>Test3</li>
<li>Test 4</li>
</ol>

<p>When the user goes to the application sometimes all are selected and sometimes only b and c are selected. I want to write a script which can do two things:</p>

<ol>
<li>Select All fields(this has be to default)</li>
</ol>

<p>If the All field is not selected, it will select it.</p>

<ol start="2">
<li>Select only one field</li>
</ol>

<p>first checks this overwrites the default which is all checkbox field. If every field is unchecked, it select the specific field.</p>

<p>this is what I tried, please bare with me on this one:</p>

<pre><code>def checkbox(checkbox):
    # checkbox variable could be a single item or multiple items user wants to check

if self.driver.find_element_by_id('type').is_selected() == False and checkbox = False:
   self.execute_script_click("id", "type")

elif self.driver.find_element_by_id('type').is_selected() == False and checkbox = True:
    # click twice to make sure nothing is selected
    self.execute_script_click("id", "type")
    self.execute_script_click("id", "type")
    for i in checkbox:
        # click is a function that finds an element and clicks the button
        self.click(10, "id", i)
</code></pre>

<p>This is what I understand from your problem description:</p>

<p>If <code>All</code> is selected do not do anything. 
If <code>All</code> is not selected, there is a flag which determines what operation to do. (In this case I will use List of Elements is empty? flag)</p>

<ol>
<li>If false --> select <code>All</code> checkbox</li>
<li><p>If true --> select the field provided by the variable <code>checkbox</code> and uncheck <code>All</code></p>

<pre><code>public void checkboxSelect(List&lt;WebElement&gt; checkboxList):
    // If All is not selected by default 
    if (driver.findElement(By.id('type')).isSelected() == false) {

        // if check box list is empty, Select All checkbox
        if (checkboxList.isEmpty()) {
            driver.findElement(By.id('type')).click();
        // else (check box list is not empty)
        } else {
            // Check all the elements in the check box list
            for (WebElement checkbox : checkboxList) {
                if (!checkbox.isSelected()) {
                    checkbox.click();
                }
            } 
        }
     }
</code></pre></li>
</ol>

<p>The snippet above is written in Java. Would'nt be difficult to convert to Python. </p>

python selenium