【问题标题】:Selenium XPATH i am trying to select a radio button from column 2 of table where text is nameSelenium XPATH 我正在尝试从表的第 2 列中选择一个单选按钮,其中文本是名称
【发布时间】:2023-03-21 23:19:01
【问题描述】:

我有一个 5 列的表格。第 2 列有一些名称文本。第 1 列是一个单选按钮。第 3、4 和 5 列是一个单选按钮。 我想从名称文本为“MaidenName”的第 3 列中选择单选按钮

我可以找到 MaidenName,然后我尝试使用 follow-sibling 转到下一列。我的 XPATH 不工作。

我的 XPATH 是:

//table[@id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body"]/tbody//tr//td//div//span[text()="MAIDENNAME"]/following-sibling::td[2]//div//span[@type="radio"]

HTML 示例是:

    <table id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body" cellspacing="0"
       style="table-layout: fixed; width: 100%;">
    <colgroup>
    <tbody>
    <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="0">
    # few trs here. I have removed. too long to paste otherwise
    <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="12">
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-1076" style="outline-style:none;">
                <span class="" title="DOB"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DOB</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-1077" style="outline-style:none;">
                <input type="radio" name="rb3386"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-1078" style="outline-style:none;">
                <input type="radio" name="rb3386"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
            <div __gwt_cell="cell-gwt-uid-1079" style="outline-style:none;">
                <input type="radio" checked="" name="rb3386"/>
            </div>
        </td>
    </tr>
    <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="13">
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
            <div __gwt_cell="cell-gwt-uid-1075" style="outline-style:none;">
                <input type="radio" name="rbCrossRow119"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
        <td class="GJPPK2LBEG GJPPK2LBFH">
        <td class="GJPPK2LBEG GJPPK2LBFH">
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
    </tr>
    <tr class="GJPPK2LBFG GJPPK2LBMG" __gwt_subrow="0" __gwt_row="14">
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG GJPPK2LBNG">
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
            <div __gwt_cell="cell-gwt-uid-1076" style="outline-style:none;">
                <span title="MAIDENNAME"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">MAIDENNAME</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
            <div __gwt_cell="cell-gwt-uid-1077" style="outline-style:none;">
                <input type="radio" name="rb3388"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
            <div __gwt_cell="cell-gwt-uid-1078" style="outline-style:none;">
                <input type="radio" name="rb3388"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH GJPPK2LBNG">
            <div __gwt_cell="cell-gwt-uid-1079" style="outline-style:none;">
                <input type="radio" checked="" name="rb3388"/>
            </div>
        </td>
    </tr>
    <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="15">
    <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="16">
    </tbody>
</table>

找到 MaidenName 后,我想向下 2 列并从 MaidenName 中选择第 2 列中的单选按钮

谢谢,里亚兹

【问题讨论】:

    标签: python-2.7 selenium xpath selenium-webdriver


    【解决方案1】:

    我会在tr 点停下来检查相应列内是否有MAIDENNAME 文本,然后在该行中获取所需的列:

    //table[@id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body"]/tbody//
        tr[td[2]/div/span/text()="MAIDENNAME"]/
            td[3]/div/input[@type="radio"]
    

    为可读性添加了换行符。

    另外请注意,我还必须修复标签名称 - 您的目标是 input 元素,而不是 span 元素。

    【讨论】:

      【解决方案2】:

      我现在已经解决了。我应该使用以下属性。它在同一 tr 行中。 following-sibling 将向下一个 tr 节点。 需要留在同一个节点。

      我的工作 XPATH 是:

      //table[@id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body"]/tbody//tr//td//div//span[text()="MAIDENNAME"]/following::td[2]//div//input
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-28
        相关资源
        最近更新 更多