Selenium ChromeDriver系统属性NullPointerException

时间:2017-07-19 18:50:35

标签: java maven selenium properties selenium-chromedriver

我用硒编写了一个maven程序,它工作正常,现在我试图外化属性,突然间我无法启动chromedriver。我得到这个NullPointerException,似乎驱动程序试图在系统属性文件中找到一些属性。

这是我的代码

Properties properties = new Properties();
properties.load(AmazonVendor.class.getClassLoader().getResourceAsStream("application.properties"));
System.setProperties(properties);

ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
            .usingDriverExecutable(new File(System.getProperty("webdriver.chrome.driver")))
            .usingAnyFreePort()
            .withEnvironment(ImmutableMap.of("DISPLAY",":99"))
            .build();

    try {
        chromeDriverService.start();
    } catch (IOException e) {
        LOGGERR.error(e);
    }


    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

这是我的application.properties

webdriver.chrome.driver=/home/cheng/chromedriver
files.download.dir=/home/cheng/Downloads/
emails.create.dir=/home/cheng/amazon_automation/emails/
java.home=/api/java/jdk1.8.0_131/jre/

这是我的错误

[WARNING] 
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:748)

Caused by: java.lang.ExceptionInInitializerError
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:326)
at com.apioutsourcing.websiteautomation.AmazonVendor.newDriver(AmazonVendor.java:222)
at com.apioutsourcing.websiteautomation.AmazonVendor.startAutomation(AmazonVendor.java:250)
at com.apioutsourcing.websiteautomation.AmazonVendor.main(AmazonVendor.java:285)
... 6 more

Caused by: java.lang.NullPointerException
at org.openqa.selenium.Platform.extractFromSysProperty(Platform.java:245)
at org.openqa.selenium.Platform.extractFromSysProperty(Platform.java:232)
at org.openqa.selenium.Platform.getCurrent(Platform.java:200)
at org.openqa.selenium.net.PortProber.<clinit>(PortProber.java:41)
... 10 more

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我已经弄明白了,似乎System.setProperties(properties)会让java忽略所有默认属性,只查找application.properties文件中的属性,所以我只使用System.setProperty来设置它们个别地,所以java仍然会知道环境的属性