我面临一个问题。我制作了一个用于捕获段落标记值的函数。对于单个段落来说,它工作得很好,但是有很多段落标签。我如何在函数中检索这些值?
public String captureText(WebDriver driver) {
String captureValue;
captureValue = driver.findElement(By.xpath("//span[text()='Subtitles']//following::p[1]")).getText();
return captureValue;
}
我的函数如上所示。
最佳答案
尝试使用以下代码:
//create a list to store all the paragraph text values
//Store all the paragraphs element in another list
public List<String> captureText(WebDriver driver) {
List<String> allParagraphsValues = new ArrayList<String>();
List<WebElement> allParagraphs = driver.findElements(By.xpath("//span[text()='Subtitles']//following::p"));
//Loop through all the Paragraph elements and add the text in a list
for(WebElement ele : allParagraphs){
allParagraphsValues.add(ele.getText());
}
return allParagraphsValues;
}
关于java - 如何捕获 Selenium 中多个段落标签的所有换行文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60943141/