Reputation: 25
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.
Upvotes: 1
Views: 540
Reputation: 5347
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");
}
}
}
Upvotes: 2
Reputation: 1
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++)
Upvotes: 0