Python Selenium选中复选框输入中的all all

时间:2017-01-25 08:48:38

标签: python selenium selenium-webdriver automated-tests

我想检查输入下拉菜单中的(SELECT ALL)复选框。我怎么能这样做?这是下拉菜单的屏幕截图。下拉图标旁边的字段ID为rvrMain_ctl00_ctl09_ctl00

enter image description here

到目前为止,这是我的代码,但没有给我带来好结果。

checkboxes = browser.find_elements_by_xpath("//input[@id='rvrMain_ctl00_ctl09_ctl00']")
for checkbox in checkboxes:
    if not checkbox.is_selected():
        checkbox.click()

另一次尝试,但仍然没有好结果。

browser.find_element_by_xpath("//input[@id='rvrMain_ctl00_ctl09_ctl00']").click()

任何帮助将不胜感激。谢谢和问候,

阿诺德

修改

如果我尝试检查字段的元素,这里是HMTL代码:

<table cellspacing="0" cellpadding="0">
   <tbody>
      <tr>
         <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="rvrMain_ctl00_ctl09_ctl03_ctl00" name="rvrMain$ctl00$ctl09$ctl03$ctl00" onclick="MVClassrvrMain_ctl00_ctl09.SetAutoPostBackOnHide();MultiValidValuesSelectAll(this, 'rvrMain_ctl00_ctl09_ctl03');" type="checkbox"><label for="rvrMain_ctl00_ctl09_ctl03_ctl00">(Select All)</label></span></td>
      </tr>
      <tr>
         <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="rvrMain_ctl00_ctl09_ctl03_ctl01" name="rvrMain$ctl00$ctl09$ctl03$ctl01" onclick="MVClassrvrMain_ctl00_ctl09.SetAutoPostBackOnHide();OnClickMultiValidValue(this, document.getElementById('rvrMain_ctl00_ctl09_ctl03_ctl00'));" type="checkbox"><label for="rvrMain_ctl00_ctl09_ctl03_ctl01">148950&nbsp;-&nbsp;PT.&nbsp;CATUR&nbsp;SENTOSA&nbsp;ADIPRANA&nbsp;-&nbsp;KOTABUMI</label></span></td>
      </tr>
      <tr>
         <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="rvrMain_ctl00_ctl09_ctl03_ctl02" name="rvrMain$ctl00$ctl09$ctl03$ctl02" onclick="MVClassrvrMain_ctl00_ctl09.SetAutoPostBackOnHide();OnClickMultiValidValue(this, document.getElementById('rvrMain_ctl00_ctl09_ctl03_ctl00'));" type="checkbox"><label for="rvrMain_ctl00_ctl09_ctl03_ctl02">148961&nbsp;-&nbsp;PT.&nbsp;CATUR&nbsp;SENTOSA&nbsp;ADIPRANA&nbsp;-&nbsp;BANDAR&nbsp;LAMPUNG</label></span></td>
      </tr>
      --- the list keeps go on and on---
      <tr>
         <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="rvrMain_ctl00_ctl09_ctl03_ctl203" name="rvrMain$ctl00$ctl09$ctl03$ctl203" onclick="MVClassrvrMain_ctl00_ctl09.SetAutoPostBackOnHide();OnClickMultiValidValue(this, document.getElementById('rvrMain_ctl00_ctl09_ctl03_ctl00'));" type="checkbox"><label for="rvrMain_ctl00_ctl09_ctl03_ctl203">320864&nbsp;-&nbsp;PT.&nbsp;LIQUID&nbsp;KENCANA&nbsp;ABADI&nbsp;-&nbsp;NIAS</label></span></td>
      </tr>
   </tbody>
</table>

我想选中标签为(Select All)的复选框。

修改

根据Dillanm的建议,我修改了我的代码并且它有效。基本上我必须首先单击下拉菜单图标,然后单击其中一个复选框。所以这是代码:

browser.find_element_by_id('rvrMain_ctl00_ctl09_ctl01').click() # this one click on the dropdown menu icon
browser.find_element_by_id('rvrMain_ctl00_ctl09_ctl03_ctl00').click() # this one click on the checkbox, either uncheck or check

1 个答案:

答案 0 :(得分:2)

在您的情况下,您错过了单击元素down arrow icon的步骤所以只需单击向下箭头,下拉项就会显示,然后必须执行检查选项。

喜欢 -

browser.find_element_by_xpath(down_arrow_icon_xpath).click()
checkbox = browser.find_element_by_id("rvrMain_ctl00_ctl09_ctl03_ctl00")
    if not checkbox.is_selected():
        checkbox.click()