27

我正在尝试xpath找到div并验证其中div是否有特定string的文本。

这是HTML

<div class="Caption">
  Model saved
</div>

<div id="alertLabel" class="gwt-HTML sfnStandardLeftMargin sfnStandardRightMargin sfnStandardTopMargin">
  Save to server successful
</div>

这是我目前正在使用的代码:

viewerHelper_.getWebDriver().findElement(By.xpath("//div[contains(@class, 'Caption' and .//text()='Model saved']"));
viewerHelper_.getWebDriver().findElement(By.xpath("//div[@id='alertLabel'] and .//text()='Save to server successful']"));

具体来说:

//div[contains(@class, 'Caption' and .//text()='Model saved']
//div[@id='alertLabel'] and .//text()='Save to server successful']
4

3 回答 3

49

要验证这一点: -

<div class="Caption">
  Model saved
</div>

写这个——

//div[contains(@class, 'Caption') and text()='Model saved']

并验证这一点: -

<div id="alertLabel" class="gwt-HTML sfnStandardLeftMargin sfnStandardRightMargin sfnStandardTopMargin">
  Save to server successful
</div>

写这个——

//div[@id='alertLabel' and text()='Save to server successful']
于 2013-09-02T10:20:24.840 回答
7

要考虑前导和尾随空格,您可能希望使用normalize-space()

//div[contains(@class, 'Caption') and normalize-space(.)='Model saved']

//div[@id='alertLabel' and normalize-space(.)='Save to server successful']

请注意,这//div[contains(@class, 'Caption') and normalize-space(.//text())='Model saved']也有效。

于 2013-09-02T12:19:35.183 回答
4

对于类和文本 xpath-

//div[contains(@class,'Caption') and (text(),'Model saved')]

对于类和 id xpath-

//div[contains(@class,'gwt-HTML') and @id="alertLabel"]
于 2017-04-25T13:12:18.100 回答