1

I have a html page with rows of check boxes. I want to find all checkoxes in the table and click it. What is the best way I can do this please? My first attempt I have identified the table which has the check boxes by it's ID

table_id = self.driver.find_element(By.ID, 'data_configuration_datamaps_ct_fields_body')

I then get all the rows

rows = table_id.find_elements(By.TAG_NAME, "tr")

I then use a for loop to iterate through the rows. In each iteration I locate the column

for row in rows:
    col_name = row.find_elements(By.TAG_NAME, "td")[0] 

I then locate the checkbox from the column and click it.

col_checkbox = col_name.find_elements(By.XPATH, "//input[@type='checkbox']")
col.checkbox.click()

I am doing this wrong. What is the best way to do this? I am using Selenium Webdriver with Python

My full code snippet is as follows:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

def delete_all_datamaps(self):
        try:
            WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'td')))
            table_id = self.driver.find_element(By.ID, 'data_configuration_datamaps_ct_fields_body')
            rows = table_id.find_elements(By.TAG_NAME, "tr")
            print "Rows length"
            print len(rows)
            for row in rows:
                # Get the columns
                col_name = row.find_elements(By.TAG_NAME, "td")[0]  # This is the 1st column
                print "col_name.text = "
                print col_name.text
                col_checkbox = col_name.find_elements(By.XPATH, "//input[@type='checkbox']")
                col.checkbox.click()
        except NoSuchElementException, e:
            return False
        return None

The HTML is:

<table id="data_configuration_datamaps_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
        <colgroup>
        <tbody>
            <tr class="GOFU2OVFG GOFU2OVMG" __gwt_subrow="0" __gwt_row="0">
                <td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG GOFU2OVNG">
                    <div __gwt_cell="cell-gwt-uid-185" style="outline-style:none;" tabindex="0">
                        <input type="checkbox" tabindex="-1"/>
                    </div>
                </td>
                <td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
                td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
                td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH GOFU2OVNG">
            </tr>
        </tbody>
        </table>

The following xpath will find the 1st checkbox

//table[@id="data_configuration_datamaps_ct_fields_body"]//tr/td/div/input

I only need to find the checkboxes inside this table.

Thanks, Riaz

4
  • 2
    What exactly is the question? Does it not work as you expect? Do you get error messages? What do you want to get and what are you actually getting? Currently not clear from your explanation... Commented Sep 10, 2015 at 21:46
  • If possible, include a link to the relevant web resource. Commented Sep 11, 2015 at 2:08
  • are your checkboxes under same structure? Commented Sep 11, 2015 at 6:29
  • Yes the checkboxes are under the same structure. It is not a public facing URL. The web application is installed on the local machine and only the clients have access. Commented Sep 11, 2015 at 13:21

1 Answer 1

4

I don't know python but I found some code and I think it will work... or at least be close enough that you can fix it but what you want is a CSS selector.

checkboxes = driver.find_elements_by_css_selector("#data_configuration_datamaps_ct_fields_body input[type='checkbox']")
for checkbox in checkboxes:
    checkbox.click()

The CSS selector, #data_configuration_datamaps_ct_fields_body input[type='checkbox'], reads like look for an element with an ID of data_configuration_datamaps_ct_fields_body that has a descendant INPUT element whose type is checkbox.

You will likely need a slight pause in the loop after you click to give the page a fraction of a second to respond to the click. You can try it without and see how it works. If you need a pause, it probably won't need to be long, maybe 50-100ms or so is what I would try.

Here is a CSS Selector reference.

Sign up to request clarification or add additional context in comments.

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.