如何将带有HTML标签的字符串放入数组或列表中?

时间:2019-05-01 21:13:35

标签: java selenium-webdriver

我有一个字符串,其中包含一些html标签,并且一个字符串中包含多个数据。我需要检查字符串中是否存在UI上的元素。 我可以删除标签,但不确定如何将字符串转换为数组或列表,以便比较。

例如,来自数据库的字符串是:

<dl style="float: left; text-align: left; width: 50%;">
 <dt>
     Note1amp;M
 </dt>
 <dd>
     - This is an example
 </dd>
 <dt>
     Note2
 </dt>
 <dd>
     - Example 2
 </dd>
 <dt>
     Note 3
 </dt>
 <dd>
      - This is example 3
 </dd>

用户界面的文字是

Note1 - This is an example

其中Note1是一个元素

这是另一个元素的例子

到目前为止,我必须删除标签并尝试将其放入列表中

public String[] verifyData(Strint txtFromDB) {
        String[] txt = new String[3];
        boolean compareValue1 = false, compareValue2 = false;
        boolean boolBack = 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); // getting a text from the UI;
                getMenu().scroll_To_View_Web_Element(itemLocatorP2);
                p2 = getUITxt(itemLocatorP2); // getting the second part text from the UI: 
                txt[0] = p1; //  Note 1
                txt[1] = p2; // - This is an example

                System.out.println("Array txt -> " + txt[0]);
            }
            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("&nbsp;", " ");
            txtFromDB = txtFromDB.replaceAll("amp;", "");
            txtFromDB = txtFromDB.replaceAll("&nbsp;", " ");
            txtFromDB = txtFromDB.replaceAll("&rsquo;s", "’s");
            txtFromDB = txtFromDB.replaceAll("&ndash;", "–");
            txtFromDB = txtFromDB.replaceAll("(?m)^[ \t]*\r?\n", "");
            System.out.println("DB Txt -> " + txtFromDB);

            String[] temp = txtFromDB.split("\\n");

            for(String x : temp) {
                System.out.println(x);
                if((x.contains(txt[0])) && (x.contains(txt[1]))) {
                    System.out.println(x + " from DB matches the UI -> " + txt[0] + txt[1]);
                    compareValue1 = true;
                    break;
                }
                else {
                    System.out.println("Still Searching.....");
                }
            }

            if(compareValue1 ) 
                boolBack = true;
            else
                boolBack = false;
        }
        else {
            System.out.println("No such element was found in the page");
            txt[0] = "Blank";
            boolBack = false;
        }
        txt[2] = Boolean.toString(boolBack);
        return txt;
        }

所以我想做的是将<dt>Note1</dt> and <dd>-This is an example</dd>作为一个字符串,如:Note 1 - This is an example放在列表或数组中,以便我可以与UI上的任何数据进行比较。

2 个答案:

答案 0 :(得分:2)

使用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");

答案 1 :(得分:0)

想出了办法,将字符串基于双行分割,然后将其存储在列表中,并检查我需要验证的元素是否包含在列表中

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("&nbsp;", " ");
            txtFromDB = txtFromDB.replaceAll("amp;", "");
            txtFromDB = txtFromDB.replaceAll("&nbsp;", " ");
            txtFromDB = txtFromDB.replaceAll("&rsquo;s", "’s");
            txtFromDB = txtFromDB.replaceAll("&ndash;", "–");
            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.....");
            }