java - Selenium 和 xpath : finding a div with a class/id and verifying text inside

标签 java testing xpath selenium

我正在尝试让 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']

最佳答案

验证这一点:-

<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']

关于java - Selenium 和 xpath : finding a div with a class/id and verifying text inside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18570622/

相关文章:

java - 通过 Spring 进行 RESTful 身份验证

java - 实时 API Java 支持和示例

java - org.springframework.web.multipart.commons.CommonsMultipartFile#transferTo() 方法内存安全吗?

java - 原生广告静音不显示的原因?

java - 测试 EJB 3.0 组件

javascript - 如何评估第一个快照

perl - 在 perl xpath find 语句中使用变量

c++ - gtest/gmock 是否有办法对类的每个实例进行 stub ?

jquery - 使用 jquery close() 查找与已知同级元素最接近的元素

xpath - 如何为 SOAP 标题和正文创建 xPath