2
我试图通过xpath获取webelement的文本。附上网页的HTML内容, 通过xpath获取webelement的文本
我用下面的代码,
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();
这引发错误“的表达是不是一个合法的表达式。‘代码:’ 12" nsresult: “0x805b0033(的SyntaxError)” 位置: “”]”
当我缩小所述搜索直到
String v = driver.findElement(By.xpath("//div[1]/div[contains(@class, 'ng-scope ngRow')]")).getText();
它的作品,我得到特定行中的所有文字。但是,当我将其扩展到第一个代码所示的列时,我会遇到错误。
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>
在遇到错误之前,您的第二个XPath查询是否可以进入DOM最远?您是否可以发布(作为代码)标记的相关部分,以便我们测试您的查询? – xDaevax 2014-08-29 15:54:16
不,第二个查询是有效的。如果我深入到第一个查询中提到的下一个级别,它将失败。 – ASANT 2014-08-29 15:56:27
带有代码的图像不可读。请用实际文字替换它们,缩进四个空格。然后,它会自动呈现为HTML。 _But_:确定HTML的_relevant_片段,不要用很多标记来轰炸我们。 – 2014-08-29 16:23:16