【问题标题】:Using Selenium an JUnit to parse HTML Document for links使用 Selenium 和 JUnit 解析 HTML 文档中的链接
【发布时间】:2019-02-09 20:37:50
【问题描述】:

NullPointerException at if (hrefAttr.contains("?"))

我遇到了问题。我正在使用 selenium 和 JUnit 来解析链接并将它们与 CSV 文件提供的链接列表进行比较。

一切都很顺利,直到我意识到我必须分别测试 URL 和查询字符串。我试图创建一个 if 语句,说明 href 属性是否包含“?”将整个 URL 拆分为一个包含两个字符串的数组。 URL 目标是索引的第一个字符串,查询字符串是索引的第二个字符串。并返回 URL 目标并将其附加到 ID。如果没有“?”在 URL 字符串中,只需返回 URL 字符串并将其附加到 ID

我认为逻辑看起来很准确,但我在第 76 行(href.contains("?") 条件所在的位置)不断返回空指针异常。代码如下:

public static ArrayList<String> getURLSFromHTML(WebDriver driver) {

    // prepares variable for array of html link URLs
    ArrayList <String> pageLinksList = new ArrayList<String>();

    // prepares array to place all of the <a></a> tags found in the HTML
    List <WebElement> aElements = driver.findElements(By.tagName("a"));

    // loops through all the <a></a> tags found in the HTML
    for (WebElement aElement : aElements) {

        /*
         * grabs the href attribute value and stores it into a variable
         * grabs the QA_ID attribute value and stores it in a variable
         * concatenates the QA_ID value with the href value and stores them in a variable
         */
        String hrefAttr = aElement.getAttribute("href");
        String QA_ID = aElement.getAttribute("QA_ID");
        String linkConcat;

        if (hrefAttr.contains("?")) {
            String[] splitHref = hrefAttr.split("\\?");
            String URL = splitHref[0];
            linkConcat = QA_ID + "_" + URL;
        } else {
            linkConcat = QA_ID + "_" + hrefAttr;
        }

        String urlIgnoreAttr = aElement.getAttribute("URL_ignore");
        String combIgnore = QA_ID + "_" + urlIgnoreAttr;
        String combIgnoreVal = "ignore";


        /*
         * if the QA_ID is not null then add value to pageLinksList
         * if URL_ignore attribute="ignore" in html, then add combIgnore value to pageLinksList
         * else add linkConcat to pageLinksList
         */
        if(!Objects.isNull(QA_ID)) {
            if (Objects.equals(urlIgnoreAttr, combIgnoreVal)) {
                pageLinksList.add(combIgnore);
            }else {
                pageLinksList.add(linkConcat);
            }
        }
    }

    System.out.println(pageLinksList);
    return pageLinksList;

}

请帮忙!

【问题讨论】:

    标签: java selenium junit nullpointerexception


    【解决方案1】:

    显而易见的解决方案是检查 null:

    
            if (hrefAttr != null && hrefAttr.contains("?")) {
                String[] splitHref = hrefAttr.split("\\?");
                String URL = splitHref[0];
                linkConcat = QA_ID + "_" + URL;
            } else {
                linkConcat = QA_ID + "_" + hrefAttr;
            }
    

    没有 href 属性的锚标记仍然有效。如果没有 html 源,我们无法解释缺少 href 属性的原因。 else 分支不会抛出 NPE,但它对 hrefAttr == null 毫无用处。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-30
      • 2016-12-18
      • 2015-12-10
      • 2016-01-28
      • 2010-09-12
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多