简体   繁体   English

如何在动态 Web 应用程序中使用 Gecko 驱动程序

[英]How to use Gecko driver in Dynamic Web Application

I am using Selenium to crawl a website using Java and Gecko driver.我正在使用 Selenium 使用 Java 和 Gecko 驱动程序来抓取网站。 When I run the project on a local Tomcat v.9 server I am able to get the path of the Gecko driver directly.当我在本地 Tomcat v.9 服务器上运行项目时,我可以直接获取 Gecko 驱动程序的路径。 But what I want is to be able to run the Gecko exe from within the project folder.但我想要的是能够从项目文件夹中运行 Gecko exe。 The image below shows the project hierarchy.下图显示了项目层次结构。 I have added the exe in the "resources" folder under "src".我已在“src”下的“resources”文件夹中添加了 exe。 How do I get the path of the driver to use it in my code?如何获取驱动程序的路径以在我的代码中使用它? Project hierarchy项目层次结构

Turns out it was pretty easy.事实证明这很容易。 I noticed that Catalina base has a similar directory to where WEB-INF is.我注意到 Catalina 基地有一个与 WEB-INF 类似的目录。 So I did the following;所以我做了以下事情;

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

    so the output is;所以 output 是;

C:\Users\<user>\EE Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1 C:\Users\<用户>\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).这意味着我必须保持这条路径直到“.metadata”部分(不包括它)。

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

I then excluded anything after the "EE Workspace" by splitting the Path String.然后,我通过拆分路径字符串排除了“EE 工作区”之后的任何内容。

  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.我连接了我从第 2 步得到的路径字符串和我知道的路径的 rest 是相同的。

  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.最后但并非最不重要的一点是,我使用我在步骤 3 中创建的最终路径字符串设置了壁虎驱动程序的系统属性。

Bear in mind.记住。 I am unaware if this is working when you extract the WAR file.当您提取 WAR 文件时,我不知道这是否有效。 For a local Tomcat server it will work.对于本地 Tomcat 服务器,它将工作。

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.如果有人可以让我知道当 WAR 导出并部署在真正的 Tomcat 服务器上时这是否有效,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM