通过xpath获取webelement的文本

时间:2014-08-29 15:49:11

标签: java firefox xpath selenium-webdriver

我正在尝试通过xpath获取webelement的文本。附件是页面的html内容, First Page

Second Page where highlighted in Green is the text(Closed) that I needed

我使用了以下代码,

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)“location:”“]'。

当我缩小搜索范围时

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>

1 个答案:

答案 0 :(得分:0)

刚刚测试了以下结果:在粘贴的HTML 3中,关闭</div>丢失了。 通过对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>

你只是在第一个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')]")) ..
第二行中的

缺少结束,应该是:

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

由于我刚刚使用XPath-Tester进行检查,并且您没有使用相同的设置,因此您应该尝试一下。希望有所帮助。