Selenium Python Xpath如何从许多嵌套的div标签中选择正确的跨度文本

里亚兹·拉达尼(Riaz Ladhani)

我有一个带有左侧菜单的网页。它由许多div标签组成。我已经注意到,当我运行Selenium Python脚本时,它没有单击我想要从左侧菜单中单击的文本。它正在点击其他东西。我的Xpath不正确。

我想找到具有父div文本“ Analysis”的文本“ Statistics”(位于div \ span标记中)

它没有单击正确的文本“统计”,因为HTML源代码中的某个地方可能还有另一个“统计”。如果我从具有文本“ Analysis”的div标签开始然后找到文本“ Statistics”,那么我将获得正确的元素。

我的Xpath是:

.//div//span[@title="Analysis"]/following::div[5]//span[text()="Statistics"]

HTML是:

    <div>
    <span class="" title="Analysis"
          style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;">Analysis</span>
</div>
</div>
</div>
</div>
</div>
<div style="overflow: hidden;">
    <div>
        <div>
            <div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="1" aria-expanded="false"
                 aria-level="2">
                <div class="GJPPK2LBIF" style="padding-left: 16px;">
                    <div class="GJPPK2LBIF GJPPK2LBKF" style="padding-left: 16px;position:relative;" onclick="">
                        <div class="GJPPK2LBJF" style="left: 0px;width: 15px;height: 15px;position:absolute;">
                            <img border="0"
                                 style="width:15px;height:15px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA7/HbnjJn53wAAAABJRU5ErkJggg==) no-repeat 0px 0px;"
                                 src="http://test1:8080/clearcore/ClearCore/clear.cache.gif"
                                 onload="this.__gwtLastUnhandledEvent=" load";"/>
                        </div>
                        <div class="GJPPK2LBLF">
                            <div style="padding-left: 22px;position:relative;zoom:1;">
                                <div style="left:0px;margin-top:-8px;position:absolute;top:50%;line-height:0px;">
                                    <img border="0"
                                         style="width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaekJggg==) no-repeat 0px 0px;"
                                         src="http://test1:8080/clearcore/ClearCore/clear.cache.gif"
                                         onload="this.__gwtLastUnhandledEvent=" load";"/>
                                </div>
                                <div>
                                    <span class="" title="Statistics"
                                          style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;">Statistics</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

谢谢,里亚兹

大卫·巴克(David Baak)

如果您具有带FirePath的FireFox,可以测试xpath并查看获得的数量和匹配的对象。例如:

//span[text()="Statistics"]

这可能会导致1个匹配节点,但也会导致更多。假设有两场比赛,而您想要的是第二场。然后,您将选择:

//span[text()="Statistics"][2]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python selenium webdriver中选择树节点跨度标签值?

Selenium Python XPath,如何只提取DIV中的文本而不提取内部跨度中的文本

Python Selenium:按文本从列表中选择

如何使用Selenium [Python]从范围中选择以下文本?

Python Selenium,查找跨度文本

Python Selenium 如何从一个标签中的多个文本中选择第一行文本

Selenium Python:从跨度下拉列表中选择元素

如何使用Selenium和Python在跨度内的标签内获取文本?

Python Selenium Xpath 获取文本

使用 Selenium、python 和 Xpath 在包含特定文本的表格行中选择特定按钮

Python Selenium xPath从div类中选择一个相对值

更改XPath Selenium Python

Python Selenium XPath后代

Python Selenium-如何在跨度后在div中获取文本

如何使用 selenium webdriver python 点击跨度文本

在 selenium python 中找不到跨度文本

Selenium Python:选择<a href="#" </a>

Python Selenium 选择输入

Python + Selenium选择问题

如何使用python selenium获取div文本?

如何使用 selenium 和 python 抓取 div 标签内的标签标签中的文本值?

如何使用 Python Selenium

从 selenium python 的下拉列表中选择

Python Selenium从“菜单”中选择“菜单”

Python Selenium从文件系统中选择文件

在Selenium Python中选择Javascript创建的元素

单击Selenium Python后从菜单中选择

Python + Selenium 从滚动框中选择 href

如何在 XPath Python Selenium 中忽略具有特定 html 标签的文本?