从包含多个文本的表格单元格中提取单个文本 Robot Framework

nhr*_*cpt 1 python jquery xpath python-2.7 robotframework

我有一个表,其中一些单元格有多个文本,每个文本都有自己的 xpath。例如,下面代码中的两个文本元素位于同一表格单元格中,但有两个 xpath :对于第一个“B17AA038”,xpath 为 // [ @id="profile_research"]/tbody/tr[1]/ td[10]/text() ,下一个实例有另一个 xpath (// [@id="profile_research"]/tbody/tr[1]/td[10]/div[2]/text())。在表中,我有一些单元格,其中一个单元格中包含许多单独的文本元素。我试图从单元格中选择一个特定的文本元素并将其用作变量,但到目前为止失败了。

举个例子,我尝试过这个:

Run keyword if  '${Row_No}'=='1'    get text    xpath=//*[@id="profile_research"]/tbody/tr[${Table_Row}]/td[10]/text()
Run Code Online (Sandbox Code Playgroud)

并收到此错误消息:

InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//*[@id="profile_research"]/tbody/tr[2]/td[10]/text()" is: [object Text]. It should be an element.
Run Code Online (Sandbox Code Playgroud)

相关html如下:

<td style="word-wrap: break-word; min-width: 120px; max-width: 120px; white-space:nowrap;">

                                            <!-- sample level pandh_id-->

                                                <!-- test level pandh_id-->

                                                        **B17AA03**8&nbsp;<span class="badge badge-info" style="background-color: white; color:black; border:1px solid #808080; margin-bottom:2px; font-size:0.5em;">2</span>
                                                        <!-- hidden print div for add test to this sample-->
                                                        <div class="hidden-print" style="padding-bottom:1.275em; padding-right:3em;">
                                                            <a href="/research_add_test/CS16EB/IN428A/R17AA038?subject_uuid=sub3c968403c0f248a89442b99ccca91fb4&amp;external_case_id=sfdgsfdg&amp;external_subject_id=sfgdsfdg&amp;subject_id=RS1001&amp;page=1&amp;study_id=pandh_ST_1&amp;some_list=Proband&amp;some_list=None" style="text-decoration:none" title="order another test to this sample">
                                                                <i class="fa fa-plus" aria-hidden="true" style="font-size:12px;"></i>
                                                                <i class="fa fa-file" aria-hidden="true"></i>
                                                            </a>

                                                        </div>
                                                     <!-- if it has more than 1-->
                                                 <!-- if sample pandh id and test pandh id matches-->

                                                <!-- test level pandh_id-->





                                                        <div class="hidden-print" style="padding-bottom:2.575em; visibility:hidden;">
                                                            **B17AA038**
                                                        </div>
                                                     <!-- if it has more than 1-->
                                                 <!-- if sample pandh id and test pandh id matches-->
                                             <!-- test level loop-->
                                         <!-- sample level loop-->
                                    </td>
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激。谢谢

Bry*_*ley 5

正如错误所暗示的那样,get text关键字需要一个定位器,该定位器会产生一个element,而不是一个元素的文本。本质上,您是在要求 Selenium 获取元素文本的文本。

解决方案是从定位器中删除text()

xpath=//*[@id="profile_research"]/tbody/tr[${Table_Row}]/td[10]
Run Code Online (Sandbox Code Playgroud)