【发布时间】:2015-01-31 20:18:22
【问题描述】:
我正在编写一段代码,希望我选择类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);
}
}
我错过了什么请帮忙。
谢谢
【问题讨论】:
-
最好显示要从中选择 XPath 的 HTML。
-
您的错误消息表明它找不到特定的文本,但它确实找到了元素
-
我已经粘贴了 HTML,文本 163.06 在那里
-
是否还有更多带有
amountCharged名称的元素存在? -
不,在整个 HTML 中,这个类只有一个跨度。当我通过 chrome reg x checker 进行操作时,它会选择跨度