使用静态WebDriver实例的java.lang.NullPointerException

时间:2018-03-07 10:58:03

标签: java selenium selenium-webdriver nullpointerexception selenium-chromedriver

我想将我的代码分成更小的函数。 但有一个问题,因为所有功能都无法使用驱动程序。 所以我宣称它是一个常数(或者有更好的方法吗?)

但在第3个功能中,它在线失败:

Select dropdown_finance_product = new Select(driver.findElement(By.xpath("//select[@id='ResultsNumber']")));

以下是控制台消息:

Exception in thread "main" java.lang.NullPointerException
    at Scraping.scrapeit.fetch_urls(scrapeit.java:49)
    at Scraping.scrapeit.main(scrapeit.java:24)

代码:

package Scraping;

import java.io.IOException;
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.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import tools.Xls_Reader;

public class scrapeit {

    static WebDriver driver;

    public static void main(String[] args) throws IOException {

        start_browser();
        fetch_urls();
        read_excel();
    }

    public static void start_browser() {

        System.setProperty("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.manage().deleteAllCookies();
        driver.get("http://www.example.com/search/items/new/");
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.titleContains("New items));
    }

    public static void fetch_urls() {

        Select dropdown_finance_product = new Select(driver.findElement(By.xpath("//select[@id='ResultsNumber']")));
        dropdown_finance_product.selectByVisibleText("100");
        System.out.println("Selected 100 Items Dropdown");

        // Open Excel and write urls back
        Xls_Reader datatable = new Xls_Reader("C:\\scrape.xlsx");

        List<WebElement> num_items = driver.findElements(
                By.xpath("//a [contains(@href,'http://www.example.com/search/items/new/latest-')] "));

        for (int i = 0; i < num_items.size(); i++) {
            System.out.println("How many items on page = : " + num_items.size() + " counter = " + i);

            String link = num_items.get(i).getAttribute("href");

            datatable.setCellData("New", "URL", i + 2, link);
            System.out.println("URL : " + link);
        }
    }

    public static void read_excel() {

        // Read in url and process...
        Xls_Reader datatable = new Xls_Reader("C:\\scrape.xlsx");
        int r = datatable.getRowCount("URL");
        int c = datatable.getColumnCount("URL");
        System.out.println("num of rows = " + r + " num of cols = " + c);
    }
}

1 个答案:

答案 0 :(得分:1)

错误说明了一切:

Exception in thread "main" java.lang.NullPointerException

class scrapeit中,您已将 webdriver 作为 WebDriver 的实例声明为静态,如下所示:

static WebDriver driver;

但是,您再次将另一个驱动程序初始化为 WebDriver 的新实例,如下所示:

WebDriver driver = new ChromeDriver();

因此,您会看到 java.lang.NullPointerException

解决方案

快速解决方案不是创建 WebDriver 的另一个实例,而是需要使用 WebDriver 静态实例。因此,您需要从WebDriver driver = new ChromeDriver();中删除 WebDriver ,如下所示:

driver = new ChromeDriver();