问题
I have a table in which some cells have multiple text and each text has its own xpath. An example, the two text elements in the code below are located in the same table cell, but has two xpaths : for the first "B17AA038" xpath is //[@id="profile_research"]/tbody/tr[1]/td[10]/text() and the next instance has another xpath (//[@id="profile_research"]/tbody/tr[1]/td[10]/div[2]/text()). In the table I have some cells that have many individual text elements packed in one cell. I am trying to select one particular text element from the cell and use it as variable , but so far failed.
as an example, I tried this:
Run keyword if '${Row_No}'=='1' get text xpath=//*[@id="profile_research"]/tbody/tr[${Table_Row}]/td[10]/text()
and got this error message:
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.
The related html is as follows:
<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 <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&external_case_id=sfdgsfdg&external_subject_id=sfgdsfdg&subject_id=RS1001&page=1&study_id=pandh_ST_1&some_list=Proband&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>
Any suggestion will be appreciated. Thanks
回答1:
As the error implies, the get text
keyword requires a locator that results in an element, not the text of an element. In essence, you are asking selenium to get the text of the text of an element.
The solution is to remove the text()
from the locator:
xpath=//*[@id="profile_research"]/tbody/tr[${Table_Row}]/td[10]
来源:https://stackoverflow.com/questions/45928572/extracting-single-text-from-a-table-cell-containing-multiple-texts-robot-framewo