Using Selenium an JUnit to parse HTML Document for links

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

I'm running into a problem. I'm using selenium and JUnit to parse through links and compare them to a list of links provided from a CSV file.

Everything was going well until I realized that I have to test the URLs and the query strings separately. I attempted to create an if statement saying that if the href attribute contained a "?" split the entire URL into an array containing two strings. The URL destination being the first string indexed and the query string being the second string indexed. and return the URL destination and append it to an ID. If there was no "?" in the URL string, just return the URL string and append it to an ID

I think the logic looks accurate but I keep returning a Null Pointer Exception at Line 76 (where the href.contains("?") condition is located. Code below:

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;

}

Please help!

Answer:1

I have used Eclipse Helios 3.6.1 for Java development. I wanted to start C++ development in the same IDE so I installed Autotools Support For CDT, C/C++ Development Tools, C/C++ Library API ...

I got java JDK 11 installed and I am using the Maven JavaFX. But when I try and run the code I get a build error which is "class file has wrong version 54, should be 52" POM.xml <dependencies&...

I am running a Java code in a server (remotely) by using javac. Now, I want to perform memory profiling remotely because as YourKit say's, Picked up JAVA_TOOL_OPTIONS: Cannot start the profiler UI: ...

I need to find and display the the employee who has the maximum salary from the Farm. this is what I got so far public class Employee implements Comparable<Employee> { private String name; ...