2

I want to use selenium + phantomJs in my Spring boot project for scrapy. I can make it work in a normal java app like this :

System.setProperty("phantomjs.binary.path", path_to_phantomjs.exe);
PhantomJSDriver driver = new PhantomJSDriver();
driver.get(a_web_url);
WebElement e = driver.findElementByXPath("//button[@title='load-more']");
e.click();
JSWaiter.setDriver(driver);
JSWaiter.waitJsJQueryAngular();
String html = driver.getPageSource();
System.out.println(html);
driver.quit();

I set phantomJs in my spring boot project like this

main
|__java
|__resources
   |__static
      |__phantomJs
         |__pahntomjs.exe

Whether I call :

public static void main(String[] args)
{
    System.setProperty("phantomjs.binary.path", "static/phantomJs/phantomjs.exe");
    SpringApplication.run(PricecompareApplication.class, args);
}

or :

@RequestMapping(value = {"/crawl"}, method = RequestMethod.POST)
public String crawlProduct(Model model, CrawlerOption crawlOption)
{
    System.setProperty("phantomjs.binary.path", "static/phantomJs/phantomjs.exe");
    PhantomJSDriver driver = new PhantomJSDriver(); // <-- always error at this
}

My program alway throw this error when i create a new PhantomJSDriver :

java.lang.IllegalAccessError: tried to access class org.openqa.selenium.os.ExecutableFinder from class org.openqa.selenium.phantomjs.PhantomJSDriverService

It seem like I set the wrong path or I can not access the resources, but when I use the path to phantomjs.exe in my console app (the first code block in my question), still the same error .

How can I config selenium + phantomjs in Spring boot?

Hai Hoang
  • 1,207
  • 2
  • 18
  • 35
  • Did you try with absolute url? If yes, I would try setting the property to the spring boot app directly: https://stackoverflow.com/questions/29072628/how-to-override-spring-boot-application-properties-programmatically – Szigyártó Mihály Dec 09 '17 at 12:52
  • Spring boot does not accept extend property, if i set `phantomjs.binary.path` in my `application.properties` , spring can not resolve it. According to [this issue](https://github.com/codeborne/selenide/issues/561) , the problem is caused by gradle. I update my build.gradle and everything work fine. – Hai Hoang Dec 09 '17 at 13:06
  • Wow, interesting. I'm glad that it's solved :) – Szigyártó Mihály Dec 09 '17 at 13:22

0 Answers0