[英]XPath suggestion needed to select text
I have a html document with following html code: 我有一个html文档,其中包含以下HTML代码:
<!DOCTYPE html> <html> <head> <title>page XYZ</title> </head> <body> <p><b>bold text</b> some more text </p> <p><b>1.</b> hello<br><b>2.</b> how<br><b>3.</b> do you do?</p><p>some more text in a paragraph.</p> </body> </html>
I need to write XPath to print 3 points with their text, ie, 我需要编写XPath来打印带有文本的3个点,即
1. hello
2. how
3. do you do?
so far I have tried this: (but it is not printing text) 到目前为止我试过这个:(但它不是打印文本)
List<WebElement> list = driver.findElements(By.xpath("//p/following-sibling::p/b"));
for (int i = 0; i < list.size(); i++) {
WebElement el = list.get(i);
String text = el.getText();
System.out.println(text);
}
Please suggest me. 请建议我。
Use the xpath "(//p[2])/text()"
which will return the text inside the sibling <p>
tag. 使用xpath "(//p[2])/text()"
将返回兄弟<p>
标签内的文本。 Use the xpath "(//p[2])/b"
to fetch the numbers 1,2,3 in bold. 使用xpath "(//p[2])/b"
以粗体显示数字1,2,3。
To get the desired output, you can try the following: 要获得所需的输出,您可以尝试以下方法:
List<WebElement> numberList = driver.findElements(By.xpath("(//p[2])/b"));
List<WebElement> textList = driver.findElements(By.xpath("(//p[2])/text()"));
for(int i = 0; i < textList.size() && i < numberList.size(); i++) {
System.out.println(numberList.get(i).getText() + " " + textList.get(i).getText());
}
To select text only you should use //p[2]/text()
要仅选择文本,您应该使用//p[2]/text()
and if you need to print 1. 2. 3. you should use p[2]/b
如果你需要打印1. 2. 3.你应该使用p[2]/b
if you need to select both you should use //p[2]
as string without .findElements()
then in your code split it to be like a list 如果你需要选择两者,你应该使用//p[2]
作为字符串,而不是.findElements()
然后在你的代码中将它拆分为一个列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.