如何使用 XPATH 搜索具有 class 的跨度以在 JAVA 中进行 Selenium 测试
How to use XPATH to search for a span with a class for Selenium Testing in JAVA
我正在编写一段代码,它希望我 select 具有 class amountCharged
的跨度并获取该跨度的值以根据模拟值对其进行测试.我面临的问题是我似乎无法 select span
使用 XPath
。我尝试了多种方法但仍然失败,我继续 W3Schools 和 Whosebug 问题,但无法确定语法。
以下是我面临的错误:
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')]
HTML 从 PARENT DIV 开始是:
<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
并不一定意味着 selector
将唯一地 return 目标元素。可能有一些其他元素隐藏在相同的选择器中,在这种情况下 webdriver
将无法找到目标元素。出于调试目的,使用 findElements
a 查看它有多少个元素 returns.
- 元素加载问题,例如:可见性也可能是另一个问题。这就是您需要确保元素存在且可见的原因。使用explicit等待
尝试关注 xpaths
//span[@class='amountCharged']
如果 class 不是唯一的,并且您想根据 div 找到 span
,您可以执行以下操作:
//div[contains(@class,'panelMessage ')]//span[@class='amountCharged']
根据您提到的错误,我想您正在使用 textToBePresentInElement or textToBePresentInElementLocated or something related method under the ExpectedConditions class。
您也可以这样做:
1- 使用 Explicit Wait
等待元素的可见性
2- 检索文本
3- Assert/Compare 值为“£163.06”的文本。
您可以按顺序尝试以下包含上述所有内容的代码:
String text_in_panel=null;
try{
//waiting 20 seconds for the visibility of the element
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class, 'panelMessage')]/span[1]")));
//Retrieving the text from the element
text_in_panel = element.getText();
}catch(Throwable e){
System.err.println("Element is not visible: "+e.getMessage());
}
//Comparing the text with the hard-coded value
if(text_in_panel.equals("£163.06"))
System.out.println("Text matches");
else
System.err.println("Text doesn't match");
[或者您可以通过导入 import junit.framework.Assert;]
使用断言 class 来断言这些值而不是比较它们
//Assert the text with the hard-coded value
try{
Assert.assertEquals(text_in_panel, "£163.06");
System.out.println("Text matches");
}catch(Throwable e){
System.err.println("Text doesn't match");
}
我正在编写一段代码,它希望我 select 具有 class amountCharged
的跨度并获取该跨度的值以根据模拟值对其进行测试.我面临的问题是我似乎无法 select span
使用 XPath
。我尝试了多种方法但仍然失败,我继续 W3Schools 和 Whosebug 问题,但无法确定语法。
以下是我面临的错误:
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')]
HTML 从 PARENT DIV 开始是:
<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
并不一定意味着selector
将唯一地 return 目标元素。可能有一些其他元素隐藏在相同的选择器中,在这种情况下webdriver
将无法找到目标元素。出于调试目的,使用findElements
a 查看它有多少个元素 returns. - 元素加载问题,例如:可见性也可能是另一个问题。这就是您需要确保元素存在且可见的原因。使用explicit等待
尝试关注 xpaths
//span[@class='amountCharged']
如果 class 不是唯一的,并且您想根据 div 找到 span
,您可以执行以下操作:
//div[contains(@class,'panelMessage ')]//span[@class='amountCharged']
根据您提到的错误,我想您正在使用 textToBePresentInElement or textToBePresentInElementLocated or something related method under the ExpectedConditions class。
您也可以这样做:
1- 使用 Explicit Wait
等待元素的可见性
2- 检索文本
3- Assert/Compare 值为“£163.06”的文本。
您可以按顺序尝试以下包含上述所有内容的代码:
String text_in_panel=null;
try{
//waiting 20 seconds for the visibility of the element
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class, 'panelMessage')]/span[1]")));
//Retrieving the text from the element
text_in_panel = element.getText();
}catch(Throwable e){
System.err.println("Element is not visible: "+e.getMessage());
}
//Comparing the text with the hard-coded value
if(text_in_panel.equals("£163.06"))
System.out.println("Text matches");
else
System.err.println("Text doesn't match");
[或者您可以通过导入 import junit.framework.Assert;]
使用断言 class 来断言这些值而不是比较它们 //Assert the text with the hard-coded value
try{
Assert.assertEquals(text_in_panel, "£163.06");
System.out.println("Text matches");
}catch(Throwable e){
System.err.println("Text doesn't match");
}