【问题标题】:I am trying to find xpath for checkbox in a table column我正在尝试在表格列中查找复选框的 xpath
【发布时间】:2015-07-14 12:27:15
【问题描述】:

我正在尝试查找复选框的 Xpath,然后使用我的 Python Webdriver 脚本单击它。 该复选框位于表格的列中。复选框的文本在另一列中。 文字是 DM。我想单击具有文本值 DM 的复选框。 我不想要具有文本值 ceb07_14_1504_06_52 的复选框

我需要一些帮助来获取 Xpath。

HTML 是:

    <table cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
    <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="0">
    <td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG">
    <div __gwt_cell="cell-gwt-uid-139" style="outline-style:none;" tabindex="0">
        <input type="checkbox" tabindex="-1"/>
    </div>
    </td>
    <td class="GAT4PNUEG GAT4PNUGG">
    <div __gwt_cell="cell-gwt-uid-140" style="outline-style:none;">
        <span class="linkhover" title="ceb07_14_1504_06_52" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">ceb07_14_1504_06_52</span>
    </div>
    </td>
    <td class="GAT4PNUEG GAT4PNUGG">
    <div __gwt_cell="cell-gwt-uid-141" style="outline-style:none;">
        <span class="" title="Selenium Webdriver added datamap" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Selenium Webdriver added datamap</span>
    </div>
    </td>
    <td class="GAT4PNUEG GAT4PNUGG GAT4PNUBH">
    <div __gwt_cell="cell-gwt-uid-142" style="outline-style:none;">
        <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
    </div>
    </td>
    </tr>
    <tr class="GAT4PNUEH GAT4PNUMG" __gwt_subrow="0" __gwt_row="1">
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUHG GAT4PNUNG">
    <div __gwt_cell="cell-gwt-uid-139" style="outline-style:none;">
        <input type="checkbox" tabindex="-1"/>
    </div>
    </td>
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUNG">
    <div __gwt_cell="cell-gwt-uid-140" 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="GAT4PNUEG GAT4PNUFH GAT4PNUNG">
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUBH GAT4PNUNG">
</tr>
</tbody>
</table>

我试过 //input[@type="checkbox" 和 tabindex="-1"] 我试过 //input[@type="checkbox"] 这并不好,因为表可能会随着数据的添加而增长。

我也试过了:

datamaps_checkbox_element = self.driver.find_element(By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody/tr/td[1]/div/input')
datamaps_checkbox_element.click()

问候,里亚兹

【问题讨论】:

    标签: python selenium xpath selenium-webdriver


    【解决方案1】:

    使用基于文本的 xpath 搜索

    //span[text()='DM']/../../..//input
    

    如果可能,我总是建议使用 css 而不是 xpath。

    对于第一个复选框

    .GAT4PNUEG.GAT4PNUGG.GAT4PNUHG>div>input 
    

    第二个复选框

    .GAT4PNUEG.GAT4PNUFH.GAT4PNUHG.GAT4PNUNG>div>input
    

    【讨论】:

      【解决方案2】:

      你的 html 有一些错误:

      line 39: the "td" tags must be closed
      line 41: you must close the "colgroup"
      

      顺便说一句:

      content="//span[text()='DM']/../../..//input"
                  try:                
                  WebDriverWait(self.driver, 10).until(lambda s: s.find_element_by_xpath(content).is_displayed())
      
              except TimeoutError:
                  logging.fatal("Timeout occurred trying to search content["+content+"]")
      self.driver.find_element_by_xpath(content).click()
      

      仅当您的代码中有多个“find_element_by_*”时才需要 try/except 块

      问候 克劳迪奥

      【讨论】:

        【解决方案3】:

        如果您想选择带有文本DM 的元素,为什么不使用带有title='DM' 的xpath

        //span[contains(.,'DM')]
        

        或复选框为

        //span[contains(.,'DM')]/../preceding::input[1]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-15
          • 2011-03-25
          • 2014-03-11
          • 1970-01-01
          • 2018-05-27
          • 1970-01-01
          相关资源
          最近更新 更多