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