1

I'm starting my adventure with Selenium and Java. I tried to create simple script that would create list of all links on site (it also checks if it's internal or external link). It lists link like I want, but then I get test failed and NPE error.

public class Link_Grab {
    private WebDriver driver;

    @BeforeClass
    public void setup() {
    System.setProperty("webdriver.chrome.driver","C:/TEST/LIB/chromedriver.exe");
        driver = new ChromeDriver();
    }

    @AfterClass
    public void teardown() {
        driver.close();
    }

    @Test
    public void grablinks() {
        driver.get("https://www.amazon.com");

        List<WebElement> links = driver.findElements(By.tagName("a"));
        for (int i = 1; i<=links.size(); i=i+1)
        {
            if((links.get(i).getAttribute("href")).contains("amazon"))
            {
                System.out.println(links.get(i).getAttribute("href") + " = internal domain");
            }
            else
            {
                System.out.println(links.get(i).getAttribute("href") + " = external domain");
            }

        }

    }
}

Here is error I get:

FAILED: grablinks
java.lang.NullPointerException
    at com.amazon.tests.Link_Grab.grablinks(Link_Grab.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

My imports:

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

Can you help me with it? I can't find reason of this error.

2
  • Your imports? Which is Line no 36? Commented Jun 19, 2018 at 7:39
  • Line #36: List<WebElement> links = driver.findElements(By.tagName("a")); I added imports to original question. Commented Jun 19, 2018 at 8:16

2 Answers 2

2

The issue here that, some of the anchor tag doesn't have href attribute and it throws null pointer exception. getAttribute method returns null if the attribute is not there. Also, the list index starts with 0. please try the following code.

@Test
public void grablinks() {

    String hrefvalue = null;

    driver.get("https://www.amazon.com");

    List<WebElement> links = driver.findElements(By.tagName("a"));
    for (int i = 0; i<links.size(); i++)
    {
        hrefvalue = links.get(i).getAttribute("href");

        if(hrefvalue != null){
            if(hrefvalue.contains("amazon"))
            {
               System.out.println( hrefvalue + " = internal domain");
            }
            else
            {
               System.out.println( hrefvalue + " = external domain");
            }
        }else{
              System.out.println("element doesn't have href attriubte");
       }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

The last entry in the list you are retrieved (i=links.size()) is not there. This can be avoided by using the Iterator from the list in the following way:

for (WebElement element : links)

or if you insist on using the index it should be

for (int i = 0; i < links.size(); i++)

4 Comments

Yes, I figured out that would be the problem when I started getting indexoutofboundsexception when I tried this script on facebook page. I have two questions. How should iterate on if((links.get(i).getAttribute("href")).contains("amazon")) if I use for (WebElement element : links)? Also now I'm getting NPE on mentioned "if" and I don't understand what's the problem now, what's wrong with this "if"?
In stead of using links.get(i) use element i.e. if(element.getAttribute("href")).contains("amazon") and do this for all future usage of links.get(i).
Thank you for explanation. I changed my code to use element, but I still get NPE error on that "if.
As Murthi pointed out below, you need to check whether or not the attribute is present before calling the .contains("amazon")method on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.