css - 使用 XPATH 的 Selenium Webdriver Python 从 html 表的第 1 列找到文本,我如何转到第 3 列

标签 css python-2.7 selenium xpath selenium-webdriver

我有一个包含一些列的 HTML 表格。我使用 XPATH 从“名称”列中找到了文本。这是列索引 [1] 从这里我想继续我的 XPATH 并转到第三列。索引 [3] 我不知道如何继续我的 XPATH 到达第三列。

我的 XPATH 是:

//table[@id="analysis_reports_ct_fields_body"]//span[contains(text(), "Matches")]

如果您查看下面的 HTML, TextView 位于第三列。我不想使用 TextView ,因为该文本有时是空白的。

HTML 是:

<table id="analysis_reports_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
    <colgroup>
        <tbody>
            <tr class="GLKP2TGBFG" __gwt_subrow="0" __gwt_row="0">
                <td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBHG">
                    <td class="GLKP2TGBEG GLKP2TGBGG">
                        <div __gwt_cell="cell-gwt-uid-695" style="outline-style:none;">
                            <span class="linkhover" title="Matches" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Matches</span>
                        </div>
                    </td>
                    <td class="GLKP2TGBEG GLKP2TGBGG">
                        <div __gwt_cell="cell-gwt-uid-696" style="outline-style:none;">
                            <span class="" title="manual" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">manual</span>
                        </div>
                    </td>
                    <td class="GLKP2TGBEG GLKP2TGBGG">
                        <div __gwt_cell="cell-gwt-uid-697" style="outline-style:none;">
                            <span class="linkhover block" title="view" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">view</span>
                        </div>
                    </td>
                    <td class="GLKP2TGBEG GLKP2TGBGG">
                        <div __gwt_cell="cell-gwt-uid-698" 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>
                    <td class="GLKP2TGBEG GLKP2TGBGG">
                        <div __gwt_cell="cell-gwt-uid-699" style="outline-style:none;">
                            <span class="" title="Source records" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Source records</span>
                        </div>
                    </td>
                    <td class="GLKP2TGBEG GLKP2TGBGG">
                        <div __gwt_cell="cell-gwt-uid-700" style="outline-style:none;">
                            <span class="" title="29/10/2015 14:47:21" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">29/10/2015 14:47:21</span>
                        </div>
                    </td>
                    <td class="GLKP2TGBEG GLKP2TGBGG">
                        <div __gwt_cell="cell-gwt-uid-701" style="outline-style:none;">
                            <span class="block" title="" style="">498</span>
                        </div>
                    </td>
                    <td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBBH">
                        <div __gwt_cell="cell-gwt-uid-702" style="outline-style:none;">
                            <span class="" title="INFOSHARE\riaz.ladhani" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Company\riaz.ladhani</span>
                        </div>
                    </td>
            </tr>
            <tr class="GLKP2TGBEH" __gwt_subrow="0" __gwt_row="1">
        </tbody>
</table>

如果我执行以下 XPATH,表中的很多部分都会突出显示

//table[@id="analysis_reports_ct_fields_body"]//span[contains(text(), "Matches")]//ancestor::div

我可以以某种方式使用祖先来到达第三列吗?

如果我尝试//descendant::* 这不会突出显示任何内容。我认为后代应该下树,祖先应该上树

//table[@id="analysis_reports_ct_fields_body"]//span[contains(text(), "Matches")]//descendant::*

我的方法将接受一个名为 Name 的参数(这是表的第 1 列,文本“Matches”位于其中),然后从这里我想找到表的第 3 列。

谢谢, 里亚兹

最佳答案

您可以使用以下 Xpath。

//table[@id='analysis_reports_ct_fields_body']//td[.//span[contains(text(), 'Matches')]]/following-sibling::td[2]

上面的 Xpath 在表中查找 td 标记,该标记包含包含文本 Matchesspan 并导航到其第二个同级(列位于索引 3)。 此 Xpath 消除了在 DOM 中遍历的需要。

关于css - 使用 XPATH 的 Selenium Webdriver Python 从 html 表的第 1 列找到文本,我如何转到第 3 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33418499/

相关文章:

html - 强制按钮显示在内容下方 IE7

javascript - 当我调整浏览器大小时,Bootstrap Navbar 不起作用

python - 使用 GRASS GIS API 的 python 脚本错误

Python 序数排序

css - 导航栏右侧不起作用

python - 属性错误 : module 'django.contrib.postgres.fields' has no attribute 'JSONField'

jquery - 无法使用 Selenium 在页面上找到 p 标记元素

python - 如何在Python中使用selenium跟踪动态更新代码

c# - 在 C# 中使用 Selenium ChromeDriver 单击下载 PDF 不起作用

javascript - 需要页面上其他 ID 的动画,包括 jsfiddle