我正在编写一段代码,它希望我选择带有类 amountCharged
的范围,并获取该范围的值以根据模拟值对其进行测试。我面临的问题是我似乎无法使用 XPath
选择该 span
。我尝试了多种方法但仍然失败,我继续了 W3Schools 和 Stackoverflow 问题,但无法确定语法。
以下是我遇到的错误:
org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for text ('£163.06') to be present in element found by `By.xpath:`
//span[contains(@class, 'amountCharged')]
如您所见,我正在尝试使用 //span[contains(@class,'amountCharged')]
作为 XPATH
,与使用“
/descendant::span[contains(@class, 'amountCharged')]
从父级 DIV 开始的 HTML 是:
<div class="panelMessage instructional" params=""> Total:
<span class="amountCharged">£163.06</span> quoted on
<span class="reservationTime">Tue, 29 Nov 2011 15:46 GMT</span> . </div>
</div>
<div class="panelContent hideFocus noneBlock " tabindex="-1" data- context="panelContent">
JAVA代码是:
private static void elementShouldContain(String locator, String value, String errorMessage, long timeout) {
String xpath = buildLocator(locator, "");
WebDriverWait customWait = new WebDriverWait(webDriverInstance, timeout / 1000);
try {
customWait.until(ExpectedConditions.textToBePresentInElement(By.xpath(xpath), value));
} catch (Exception e) {
// Handles the specific exception, except that the message is null, in which case throws a regular SeleniumException
if (errorMessage != null)
throw new CustomSeleniumException(errorMessage, e);
else
throw new SeleniumException(e);
}
}
我缺少什么请帮忙。
谢谢
最佳答案
需要记住以下几点:
- 在 Firebug 的 Chrome 调试器上测试
xpath
并不一定意味着选择器
将唯一地返回目标元素。可能有一些其他元素隐藏在相同的选择器中,在这种情况下,webdriver
将无法找到目标元素。出于调试目的,请使用findElements
查看它返回了多少个元素。 - 元素加载问题,例如:可见性也可能是另一个问题。这就是为什么您需要确保该元素存在且可见。使用explicit等等
尝试遵循xpath
//span[@class='amountCharged']
如果类不是唯一的,并且您想根据 div 查找 span
,您可以执行以下操作:
//div[contains(@class,'panelMessage ')]//span[@class='amountCharged']
关于java - 如何使用 XPATH 搜索包含 JAVA 中的 Selenium 测试类的跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256095/