selenium - selenium-maven:如何从资源文件夹加载 Firefox 或浏览器驱动程序
问题描述
我在硒工作。这是演示项目。我正在使用 Maven 进行依赖管理。我已经下载了 Gecko 驱动程序并保存在本地驱动器中的文件夹位置。但它会创建与本地机器的依赖关系。
我们在 Maven 中有一个文件夹,称为资源。我想用那个。我想将 Firefox 驱动程序保留在那里并想从这里加载它。
Maven 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>maven-selemium</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Add Following Lines in Your POM File -->
<properties>
<selenium.version>2.53.1</selenium.version>
<testng.version>6.9.10</testng.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Java 代码
public class PG1 {
public static void main(String[] args) throws Exception {
String baseUrl = "https://www.google.co/";
// System.setProperty("webdriver.gecko.driver", "C:/Users/Dell/Downloads/geckodriver-v0.29.1-win64/geckodriver.exe");
System.setProperty("webdriver.gecko.driver", "E:/Drive_H/projects/selenium/src/main/resources/geckodriver-firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.close();
}
}
我试过这条线,但错误:无法加载驱动程序。
System.setProperty("webdriver.gecko.driver", "geckodriver-firefox.exe");
解决方案
用于System.getProperty("user.dir")
获取项目用户目录路径,然后访问所需的可执行文件。
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\src\\main\\resources\\geckodriver-firefox.exe");
推荐阅读
- jboss - 如何使用undertow将非www URL重定向到jboss中的www URL
- powershell - PowerShell 是否会根据需要自动将管道对象转换为字符串?
- typescript - 如何使用泛型声明默认参数
- javascript - 使用 AlpineJS 在两个不同组件之间重用某些方法的最佳方法是什么?
- javascript - 如何在 javascript 中从 finder 或文件资源管理器将媒体文件复制到 Web 浏览器剪贴板
- c# - ASP.NET 在后面的代码中获取 DataList 输入
- python-3.x - 添加一个标志,指示每个组的列已更改,除非组只有一行
- string - PyMongo 在具有许多字段的大型集合中取消设置空字符串字段
- r - 如何在 R 中绘制正向/反向传播权重的轨迹图
- node.js - Webpack:将 webpack.config.js 移动到文件夹会破坏 webpack