简体   繁体   English

通过xpath获取Webelement的文本

[英]Getting the text of a webelement through xpath

I'm trying to get the text of webelement through xpath. 我正在尝试通过xpath获取webelement的文本。 Attached are the html content of the page, 附件是页面的html内容, 第一页

我需要的文本(已关闭)以绿色突出显示的第二页

I used the following code, 我使用了以下代码,

String v = driver.findElement(By.xpath("//div[1]/div[contains(@class, 'ng-scope ngRow')]//div[contains(@class, 'ngCell ng-scope col7 colt7')//"
                + "div[contains(@class, 'ngCellText ng-scope ngCellElement col7 colt7')]")).getText();

This throws the error 'The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: ""]'. 这将引发错误“表达式不是合法表达式。”代码:“ 12” nsresult:“ 0x805b0033(SyntaxError)”位置:“”]'。

When I downsized the search till 当我缩小搜索范围直到

String v = driver.findElement(By.xpath("//div[1]/div[contains(@class, 'ng-scope ngRow')]")).getText();

it works and I'm getting all the text in the particular row. 它的工作原理,我得到特定行中的所有文本。 However, when I extend it specific to a column as shown in the 1st code, I get error. 但是,当我将其扩展到第一个代码中所示的特定列时,出现错误。

html snippet, html片段,

<div class="ng-scope ngRow even" ng-row="" ng-class="row.alternatingRowClass()" ng-click="row.toggleSelected($event)" ng-repeat="row in renderedRows" ng-style="rowStyle(row)" style="top: 0px; height: 30px;">
<div class="ngCell ng-scope pinned col0 colt0" ng-class="col.colIndex()" ng-repeat="col in renderedColumns" ng-style="{ 'cursor': row.cursor }" ng-dblclick="loadInstance(row.getProperty('incident_number'), 'ticket')" style="cursor: default;">
<div class="ngCell ng-scope col7 colt7" ng-class="col.colIndex()" ng-repeat="col in renderedColumns" ng-style="{ 'cursor': row.cursor }" ng-dblclick="loadInstance(row.getProperty('incident_number'), 'ticket')" style="cursor: default;">
<div class="ng-isolate-scope" data-target="actions_menu" context-menu="">
<div ng-style="{ 'cursor': row.cursor }" style="cursor: default;">
<div class="ngVerticalBar ngVerticalBarVisible" ng-class="{ ngVerticalBarVisible: !$last }"/>
<div ng-cell="">
<div class="ngCellText ng-scope ngCellElement col7 colt7" ng-class="col.colIndex()" tabindex="0">
<span class="ng-binding" ng-cell-text="">Closed</span>
</div>
</div>
</div>
</div>
</div>

Just tested it out with following results: in the pasted HTML 3 closing </div> are missing. 刚刚测试了以下结果:在粘贴的HTML 3中, </div>关闭。 With a minimal adjustment of your XPath, I get following result: 通过对XPath的最小调整,我得到以下结果:

<div class="ngCellText ng-scope ngCellElement col7 colt7" 
 ng-class="col.colIndex()"  tabindex="0">
 <span class="ng-binding" ng-cell-text="">Closed</span>
</div>

You're just missing a ] in the first XPath, then it should work: 您仅在第一个XPath中缺少一个],那么它应该可以工作:

"//div[1]/div[contains(@class, 'ng-scope ngRow')]
//div[contains(@class, 'ngCell ng-scope  col7 colt7')//"
+ "div[contains(@class, 'ngCellText ng-scope ngCellElement col7 colt7')]")) ..

in the 2nd line the closing ] is missing, it should be: 在第二行中,缺少[],应该是:

"//div[1]/div[contains(@class, 'ng-scope ngRow')]
//div[contains(@class, 'ngCell ng-scope  col7 colt7')]//"

As I just used an XPath-Tester for checking and don't have the same setting you're working on, you should just give it a try. 由于我只是使用XPath-Tester进行检查,并且没有与您正在使用的相同的设置,因此您应该尝试一下。 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM