Selenium Webdriver Python 我如何 select 一个特定的复选框
Selenium Webdriver Python how do i select a particular checkbox
我有一个包含一些行和列的 html table。每行的第一列都有一个复选框。第 2 列中的名称。
我想 select 名为 test2 的复选框(我将以 test2 为例)
我尝试使用 for 循环遍历 table 和行,如果找到名称 test2,则单击复选框。
我的代码没有点击复选框。
我的代码片段是:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def select_datamap(self, datamap_name):
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")
for row in rows:
print row
# Get the columns
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Name column
print col_name.text
if (col_name.text == datamap_name):
checkbox = self.driver.find_element(By.XPATH, '//table[@id="data_configuration_datamaps_ct_fields_body"]/tbody/tr[%s]/td[1]/div/input') % row
#checkbox = self.driver.find_element_by_css_selector("#data_configuration_datamaps_ct_fields_body input[type='checkbox']")
checkbox.click()
return True
return False
except NoSuchElementException, e:
return False
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">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
<span class="linkhover" title="DM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">DM</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH GOFU2OVNG">
</tr>
<tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="1">
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVHG">
<div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVFH">
<div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
<span class="linkhover" title="test1" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test1</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVFH">
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVBH">
</tr>
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="2">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG">
<div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG">
<div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
<span class="linkhover" title="test2" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test2</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH">
</tr>
</tbody>
</table>
从我的 TestCase class 我用参数 "test1"
调用 select_datamap 方法
import unittest
some more imports ...
class DatamapsPage_TestCase(BaseTestCase):
def test_delete_datamaps(self):
print "*** Test Delete DM Datamaps ***"
some steps ...
...
tool_bar = ToolbarPage(self.driver)
dm = tool_bar.clickMoreTools("Data Configuration", "Datamaps" )
dm.select_datamap("test1")
如何找到与我传递的参数匹配的复选框,然后单击它?
使用 for 循环会很好,因为参数 test2 可以在任何行中,具体取决于客户端在应用程序中输入的名称。
谢谢,
实际上你可以使用下面的 xpath 直接找到复选框而不需要迭代:
//span [text()='test2']/../../preceding-sibling::td/div/input[@type='checkbox']
xpath 表达式仍然不是很好,但它应该可以工作:)
然后在结果上做一个.click()
我有一个包含一些行和列的 html table。每行的第一列都有一个复选框。第 2 列中的名称。 我想 select 名为 test2 的复选框(我将以 test2 为例) 我尝试使用 for 循环遍历 table 和行,如果找到名称 test2,则单击复选框。 我的代码没有点击复选框。
我的代码片段是:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def select_datamap(self, datamap_name):
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")
for row in rows:
print row
# Get the columns
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Name column
print col_name.text
if (col_name.text == datamap_name):
checkbox = self.driver.find_element(By.XPATH, '//table[@id="data_configuration_datamaps_ct_fields_body"]/tbody/tr[%s]/td[1]/div/input') % row
#checkbox = self.driver.find_element_by_css_selector("#data_configuration_datamaps_ct_fields_body input[type='checkbox']")
checkbox.click()
return True
return False
except NoSuchElementException, e:
return False
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">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
<span class="linkhover" title="DM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">DM</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH GOFU2OVNG">
</tr>
<tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="1">
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVHG">
<div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVFH">
<div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
<span class="linkhover" title="test1" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test1</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVFH">
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVBH">
</tr>
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="2">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG">
<div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG">
<div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
<span class="linkhover" title="test2" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test2</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH">
</tr>
</tbody>
</table>
从我的 TestCase class 我用参数 "test1"
调用 select_datamap 方法import unittest
some more imports ...
class DatamapsPage_TestCase(BaseTestCase):
def test_delete_datamaps(self):
print "*** Test Delete DM Datamaps ***"
some steps ...
...
tool_bar = ToolbarPage(self.driver)
dm = tool_bar.clickMoreTools("Data Configuration", "Datamaps" )
dm.select_datamap("test1")
如何找到与我传递的参数匹配的复选框,然后单击它? 使用 for 循环会很好,因为参数 test2 可以在任何行中,具体取决于客户端在应用程序中输入的名称。
谢谢,
实际上你可以使用下面的 xpath 直接找到复选框而不需要迭代:
//span [text()='test2']/../../preceding-sibling::td/div/input[@type='checkbox']
xpath 表达式仍然不是很好,但它应该可以工作:)
然后在结果上做一个.click()