2 回答
TA贡献1841条经验 获得超3个赞
用JSoup
您可以考虑使用 JSoup,而不是自己解析它。
https://en.wikipedia.org/wiki/Jsoup
使用 JSoup,您可以删除所有 html 并通过以下方式获取文本:
String html = "<p>example</p>";
Document doc = Jsoup.parse(html);
System.out.println(doc.text()); // doc.text() returns the text only from the html
这将输出:
example
使用 JSoup,您还可以找到具有特定 id 的元素,以便更轻松地将它们分开。
String html = "<dt>example</dt>";
Document doc = Jsoup.parse(html);
Elements dts = doc.getElementsByClass("dt");
TA贡献1827条经验 获得超7个赞
有点想通了,根据双线拆分字符串,然后将其存储在一个列表中,并检查我需要验证的元素是否包含在该列表中
public void verifyEachCommonAcronymsAndAbbreviationsForAPB(String itemLocatorP1, String itemLocatorP2, String txtFromDB) {
String[] txt = new String[3];
boolean compareValue1 = false, compareValue2 = false;
WebElement abbreviation = driver.findElement(By.xpath(itemLocatorP1));
WebElement fullName = driver.findElement(By.xpath(itemLocatorP2));
String p1, p2;
if((abbreviation.isDisplayed()) && (fullName.isDisplayed())) {
try {
getMenu().scroll_To_View_Web_Element(itemLocatorP1);
p1 = getUITxt(itemLocatorP1);
getMenu().scroll_To_View_Web_Element(itemLocatorP2);
p2 = getUITxt(itemLocatorP2);
txt[0] = p1;
txt[1] = p2;
}
catch(Exception e) {
txt[0] = "Blank";
System.out.println("Array txt Exception-> " + txt[0]);
}
// removing some html txt from the txtFromDB so that it can match with the UI
txtFromDB = txtFromDB.replaceAll("<dt>", "");
txtFromDB = txtFromDB.replaceAll("</dt>", "");
txtFromDB = txtFromDB.replaceAll("<dd>", "");
txtFromDB = txtFromDB.replaceAll("</dd>", "");
txtFromDB = txtFromDB.replaceAll(">", "");
txtFromDB = txtFromDB.replaceAll("</dl>", "");
txtFromDB = txtFromDB.replaceAll("</dl", "");
txtFromDB = txtFromDB.replaceAll("<dl style=", "");
txtFromDB = txtFromDB.replaceAll("float: left; text-align: left; width: 50%;", "");
txtFromDB = txtFromDB.replaceAll("\"\"", "");
txtFromDB = txtFromDB.replaceAll(" ", " ");
txtFromDB = txtFromDB.replaceAll("amp;", "");
txtFromDB = txtFromDB.replaceAll(" ", " ");
txtFromDB = txtFromDB.replaceAll("’s", "’s");
txtFromDB = txtFromDB.replaceAll("–", "–");
txtFromDB = txtFromDB.replaceAll("(?m)^[ \t]*\r?\n", "");
//System.out.println("DB Txt -> " + txtFromDB);
String[] splitArrDB = txtFromDB.split("\\n");
List<String> acronymsList = new ArrayList<>();
for(int i = 0 ; i < splitArrDB.length; i++) {
acronymsList.add(splitArrDB[i] + splitArrDB[i]);
}
for(String temp : acronymsList) {
if((temp.contains(txt[0]))) {
System.out.println("Found " + txt[0] + " in the list");
compareValue1 = true;
break;
}
//System.out.println("still searching.....");
}
for(String x : acronymsList) {
if((x.contains(txt[1]))) {
System.out.println("Found " + txt[1] + " in the list");
compareValue1 = true;
break;
}
//System.out.println("still searching.....");
}
添加回答
举报