首页 > 解决方案 > 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");

标签: seleniummaven

解决方案


用于System.getProperty("user.dir")获取项目用户目录路径,然后访问所需的可执行文件。

System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\src\\main\\resources\\geckodriver-firefox.exe");

推荐阅读