如何使用 XPATH 在 JAVA 中搜索带有 Selenium 测试类的跨度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28256095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use XPATH to search for a span with a class for Selenium Testing in JAVA
提问by alyn000r
I am working on a piece of code which wants me to select the span with the class amountCharged
and get the value of that span to test it against the mock value. The problem that I am facing is that I cannot seem to select that span
using XPath
. I have tried multiple ways and still fail, I went on W3Schoolsand Stackoverflowquestions but was unable to nail the syntax.
我正在编写一段代码,它希望我选择带有类的跨度amountCharged
并获取该跨度的值以针对模拟值对其进行测试。我面临的问题是我似乎无法选择span
使用XPath
. 我尝试了多种方法但仍然失败,我继续回答W3Schools和Stackoverflow问题,但无法确定语法。
Below is the error I am facing:
以下是我面临的错误:
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')]
As you can see I am trying to use //span[contains(@class,'amountCharged')]
as the XPATH
, same issue with using "
正如你可以看到我想使用//span[contains(@class,'amountCharged')]
的XPATH
,同样的问题用“
/descendant::span[contains(@class, 'amountCharged')]
THE HTML STARTING FROM A PARENT DIV IS:
从父 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">
The JAVA code is:
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);
}
}
what am I missing please help.
我错过了什么请帮忙。
Thank You
谢谢你
采纳答案by Subh
From the error you've mentioned, I suppose you are using textToBePresentInElementor textToBePresentInElementLocatedor something related method under the ExpectedConditions class.
从您提到的错误来看,我想您在ExpectedConditions 类下使用textToBePresentInElement或textToBePresentInElementLocated或相关方法。
You can alternatively do this:
1- Wait for the visibility of the element using Explicit Wait
2- Retrieve the text
3- Assert/Compare the text with the value: '£163.06'.
您也可以这样做:
1- 使用显式等待等待元素的可见性
2- 检索文本
3- 断言/比较文本与值:'£163.06'。
You can try the below code that comprises of all above in order:
您可以按顺序尝试包含以上所有内容的以下代码:
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");
[OR You can assert the values instead of comparing them, using the Assert class by importing import junit.framework.Assert;]
[或您可以通过导入 import junit.framework.Assert; 使用 Assert 类来断言值而不是比较它们]
//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");
}
回答by Saifur
Couple of things to keep that in mind:
请记住以下几点:
- Testing the
xpath
on Chrome debugger of Firebug does not necessarily mean that theselector
is going to return the target element uniquely. There might be some other elements hidden with same selector andwebdriver
will fail to find the target element in that case. For debugging purpose usefindElements
a see how many elements it returns. - Element loading issue such as: visibility can also be another issue. That's why you need to make sure the element is present and visible. Use explicitwait
- 测试
xpath
Firebug 的 Chrome 调试器并不一定意味着它将selector
唯一地返回目标元素。可能还有一些其他元素隐藏在相同的选择器中,webdriver
在这种情况下将无法找到目标元素。出于调试目的,请使用findElements
查看它返回多少个元素。 - 元素加载问题,例如:可见性也可能是另一个问题。这就是为什么您需要确保元素存在且可见的原因。使用显式等待
Try following xpaths
尝试以下 xpaths
//span[@class='amountCharged']
If the class is not unique and you want to find the span
depending on the div you can do the following:
如果该类不是唯一的,并且您想span
根据 div查找该类,则可以执行以下操作:
//div[contains(@class,'panelMessage ')]//span[@class='amountCharged']