我正在尝试捕获文本(网页上不同面板的标题)并验证文本内容。我尝试使用xpath捕获文本,但令人惊讶的是在WebDriver上,它失败了。相同的xpath似乎与Selenium RC完美配合。我想知道为什么WebDriver似乎没有识别出路径,而RC呢!我尝试过的代码如下:
assertEquals("Identification requests", driver.findElement(By.xpath("//span[8]")).getText());
我也尝试了完整的xpath:
assertEquals("Identification requests", driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div/div[1]/div/div[2]/div[2]/div/div/kibana-panel/div/div[1]/div[2]/div/span[8]")).getText());
我也尝试使用类变量,但这些似乎都没有返回任何结果。所有尝试都失败,并显示未找到元素的错误。
该部分的html代码如下所示:
<span class="panel-text panel-title ng-binding">Identification requests</span>
上面提到的跨度部分的HTML代码如下所示:
<div class="row-fluid panel-extra">
<div class="panel-extra-container">
<span class="extra row-button" ng-show="panel.editable != false && panel.removable != false">
<span class="extra row-button" ng-hide="panel.draggable == false">
<span class="row-button extra" ng-show="panel.editable != false">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra" ng-show="panelMeta.loading == true" style="display: none;">
<span class="panel-text panel-title ng-binding">Identification requests</span>
</div>
</div>
</div>
我怀疑为什么适用于RC的xpath对WebDriver不起作用。我将如何捕获文本?
答案 0 :(得分:0)
我建议使用以下内容(使用css选择器):
String spanCssSelector ="span.panel-text.panel-title.ng-binding";
WebElement spanItem=driver.findElement(By.cssSelector(spanCssSelector));
String textObtained= spanItem.getText().trim();
assertEquals(textObtained,"Identification requests");
希望这适合你。
答案 1 :(得分:0)
您可以使用assert
assertEquals("Identification requests", driver.findElement(By.xpath("//span[starts-with(@class, 'panel-text')]")).getText());
或
assertEquals("Identification requests", driver.findElement(By.xpath("//span[@class='panel-text panel-title ng-binding']")).getText());
或者只是通过Xpath的text()函数找到findElement:
driver.findElement(By.xpath("//span[starts-with(@class, 'panel-text')][text()='Identification requests']));
或通过完整的类属性值:
driver.findElement(By.xpath("//span[@class='panel-text panel-title ng-binding'][text()='Identification requests']));