Java,Selenium中关于链接抓取器的java.lang.NullPointerException

时间:2018-06-19 07:28:05

标签: java selenium selenium-webdriver nullpointerexception webdriver

我开始使用Selenium和Java开始冒险。我试图创建一个简单的脚本,它将创建站点上所有链接的列表(它还会检查它是内部链接还是外部链接)。它列出了我想要的链接,但后来我得到了测试失败和NPE错误。

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");
            }

        }

    }
}

这是我得到的错误:

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)

我的进口商品:

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;
你可以帮帮我吗?我找不到这个错误的原因。

2 个答案:

答案 0 :(得分:2)

这里的问题是,某些锚标记没有 href 属性,并且它会抛出空指针异常。如果属性不存在,则getAttribute方法返回null。此外,列表索引从0开始。请尝试以下代码。

@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");
       }
    }

}

答案 1 :(得分:0)

您检索的列表中的最后一个条目(i = links.size())不存在。通过以下方式使用列表中的迭代器可以避免这种情况:

for (WebElement element : links)

或者如果你坚持使用索引它应该是

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