Selenium Webdriver Python 如何单击 table 行中的所有复选框
Selenium Webdriver Python How to click all checkboxes in a table of rows
我有一个包含多行复选框的 html 页面。我想在 table 中找到所有复选框并单击它。
请问最好的方法是什么?
我的第一次尝试是通过 ID
确定了 table,其中包含复选框
table_id = self.driver.find_element(By.ID, 'data_configuration_datamaps_ct_fields_body')
然后我得到所有的行
rows = table_id.find_elements(By.TAG_NAME, "tr")
然后我使用 for 循环遍历行。在每次迭代中,我都找到列
for row in rows:
col_name = row.find_elements(By.TAG_NAME, "td")[0]
然后我从列中找到复选框并单击它。
col_checkbox = col_name.find_elements(By.XPATH, "//input[@type='checkbox']")
col.checkbox.click()
我做错了。做这个的最好方式是什么?
我正在使用带有 Python
的 Selenium Webdriver
我的完整代码片段如下:
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
HTML是:
<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>
以下 xpath 将找到第一个复选框
//table[@id="data_configuration_datamaps_ct_fields_body"]//tr/td/div/input
我只需要找到这个 table 里面的复选框。
谢谢,
里亚兹
我不知道 python 但我找到了一些代码,我认为它可以工作......或者至少足够接近你可以修复它但你想要的是 CSS选择器。
checkboxes = driver.find_elements_by_css_selector("#data_configuration_datamaps_ct_fields_body input[type='checkbox']")
for checkbox in checkboxes:
checkbox.click()
CSS 选择器 #data_configuration_datamaps_ct_fields_body input[type='checkbox']
看起来像是查找 ID 为 data_configuration_datamaps_ct_fields_body
的元素,该元素具有 INPUT
后代元素 type
是 checkbox
.
点击后您可能需要在循环中稍作停顿,让页面有几分之一秒的时间来响应点击。您可以在没有的情况下尝试它,看看它是如何工作的。如果你需要暂停,它可能不需要很长时间,我可能会尝试 50-100 毫秒左右。
我有一个包含多行复选框的 html 页面。我想在 table 中找到所有复选框并单击它。 请问最好的方法是什么? 我的第一次尝试是通过 ID
确定了 table,其中包含复选框table_id = self.driver.find_element(By.ID, 'data_configuration_datamaps_ct_fields_body')
然后我得到所有的行
rows = table_id.find_elements(By.TAG_NAME, "tr")
然后我使用 for 循环遍历行。在每次迭代中,我都找到列
for row in rows:
col_name = row.find_elements(By.TAG_NAME, "td")[0]
然后我从列中找到复选框并单击它。
col_checkbox = col_name.find_elements(By.XPATH, "//input[@type='checkbox']")
col.checkbox.click()
我做错了。做这个的最好方式是什么? 我正在使用带有 Python
的 Selenium Webdriver我的完整代码片段如下:
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
HTML是:
<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>
以下 xpath 将找到第一个复选框
//table[@id="data_configuration_datamaps_ct_fields_body"]//tr/td/div/input
我只需要找到这个 table 里面的复选框。
谢谢, 里亚兹
我不知道 python 但我找到了一些代码,我认为它可以工作......或者至少足够接近你可以修复它但你想要的是 CSS选择器。
checkboxes = driver.find_elements_by_css_selector("#data_configuration_datamaps_ct_fields_body input[type='checkbox']")
for checkbox in checkboxes:
checkbox.click()
CSS 选择器 #data_configuration_datamaps_ct_fields_body input[type='checkbox']
看起来像是查找 ID 为 data_configuration_datamaps_ct_fields_body
的元素,该元素具有 INPUT
后代元素 type
是 checkbox
.
点击后您可能需要在循环中稍作停顿,让页面有几分之一秒的时间来响应点击。您可以在没有的情况下尝试它,看看它是如何工作的。如果你需要暂停,它可能不需要很长时间,我可能会尝试 50-100 毫秒左右。