Search code examples
javaeclipsegeckodriver

How to use Gecko driver in Dynamic Web Application


I am using Selenium to crawl a website using Java and Gecko driver. When I run the project on a local Tomcat v.9 server I am able to get the path of the Gecko driver directly. But what I want is to be able to run the Gecko exe from within the project folder. The image below shows the project hierarchy. I have added the exe in the "resources" folder under "src". How do I get the path of the driver to use it in my code? Project hierarchy


Solution

  • Turns out it was pretty easy. I noticed that Catalina base has a similar directory to where WEB-INF is. So I did the following;

    1. String catalinaBase= System.getProperty("catalina.base");

      so the output is;

    C:\Users\<user>\EE Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1

    Which means I would have to keep this Path up until the ".metadata" part (excluding it).

    1. catalinaBase = catalinaBase.split(".metadata")[0];

    I then excluded anything after the "EE Workspace" by splitting the Path String.

    1. mainPath = catalinaBase + "\\Crawler\\src\\main\\webapp\\WEB-INF\\geckodriver.exe";

    I concatenated the Path String I got from step no.2 and the rest of the path I know is the same.

    1. System.setProperty("webdriver.gecko.driver",mainPath);

    Last but not least I set the system property for gecko driver using the final Path String I created on step 3.

    Bear in mind. I am unaware if this is working when you extract the WAR file. For a local Tomcat server it will work.

    If someone can let me know if this works when a WAR is exported and deployed on a real Tomcat server I would appreciate it.