首页 文章

使用Xpath Contains函数查找包含文本的元素

提问于
浏览
0

我一直在使用Contains函数为我的selenium Xpath定位器 . 到目前为止,此功能始终有效 . 它目前不适用于表中的TD元素 . 我正在向函数发送正确的文本,所以我不明白为什么 .

在Chrome上,请转到此处:https://rcpsc.releasecandidate-community360qa.net/login.aspx?action=enablelogin

登录:mj4 / test

向下滚动到“计划学习计划”选项卡

对象检查“转换为纪律(4)”的行 . 您将看到此元素是一个TD元素,其innerText为“Transition to Discipline” .

现在,按开发者工具的“元素”选项卡中的Ctrl F,按字符串,选择器或Xpath查找 . 键入以下内容以尝试通过Xpath查找:// td [contains(text(),'Discipline')]它找不到该元素 . 为什么是这样?

现在输入// * [contains(text(),'Discipline')]它找到一个带有这个文本的DIV元素,但这不是我想要/需要的元素 .

我怎样才能找到td元素 . 请注意,我需要使用文本找到此元素 .

这是我找到元素的代码:

string xpath = string.Format("//td[contains(text(),'{0}')]", "Discipline");
IWebElement elemToExpandOrCollapse = PrgLrnPlanTbl.FindElement(By.XPath(xpath));

这是HTML:

<tbody ng-repeat="stage in vm.Stages.PortfolioEPAStages" class="ng-scope">
    <tr>
        <td class="text-left ng-binding" colspan="2">
            <!-- ngIf: stage.EPACount --><span ng-if="stage.EPACount" class="ng-scope"><span role="button" ng-hide="vm.isStageExpand==true &amp;&amp; stage.StageId==vm.selectedStageId" ng-click="vm.toggleStages(stage.StageId)" tabindex="0" cbd-enter="" first-tab="" class="ng-hide"><img class="" src="/cbd/Content/images/icons/icon.expand.png" alt="Expand"></span> <span role="button" ng-show="vm.isStageExpand==true &amp;&amp; stage.StageId==vm.selectedStageId" ng-click="vm.toggleStages()" tabindex="0" cbd-enter="" first-tab="" class=""><img class="" src="/cbd/Content/images/icons/icon.collapse.png" alt="Collapse"> </span> </span><!-- end ngIf: stage.EPACount --><!-- ngIf: stage.EPACount <=0 -->Transition to Discipline (4)
        </td>
    </tr>
    <!-- ngIf: stage.StageId==vm.selectedStageId --><tr ng-if="stage.StageId==vm.selectedStageId" class="ng-scope">
        <!-- ngIf: stage.EPACount>0 --><td colspan="2" ng-if="stage.EPACount>0" style="padding:0px;" class="ng-scope">
            <!--<div class=" row">
            <div class="col-md-12">
                -->

2 回答

  • 1

    您的XPath不起作用,因为字符串“Discipline”不会出现在td元素的第一个文本节点子节点中,而是出现在后续文本节点中 . 你正在使用一个XPath 1.0处理器(天堂只知道为什么Selenium不会转向更现代的东西),而在XPath 1.0中规则是包含(X,Y),当X是一组节点时,只考虑第一个该集合中的节点,并忽略其他节点 .

    我原以为最接近你声明的要求是 //td[contains(., 'Discipline')]

  • 0

    请试试这个:

    //td[@class='text-left ng-binding' and ./span[contains(.,'Discipline')]]
    

    要么

    //td[@class='text-left ng-binding' and ./span/text()='Transition to Discipline (4)']
    

相关问题