有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Maven使用Selenium 3.4.0启动FireFoxDriver?

我试图在maven项目中使用Selenium的最新版本3.4.0。我使用以下依赖项导入了Selenium的所有JAR:-

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.4.0</version>
</dependency>

问题是,我无法解决Eclipse中我的项目对主方法中以下代码的任何依赖关系:-

public class FirefoxTest {

    public static void main(String[] args) {
        FirefoxOptions options = new FirefoxOptions();
        options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //This is the location where you have installed Firefox on your machine

        FirefoxDriver driver = new FirefoxDriver(options);
        driver.get("http://www.google.com");
    }
}

我错过了什么?Eclipse无法将FirefoxDriver类型解析为任何依赖项。请帮忙


共 (6) 个答案

  1. # 1 楼答案

    使用以下依赖项下载selenium

    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
    </dependency>
    

    下载依赖项后。执行构建项目

    这将解决您的问题

  2. # 2 楼答案

    将此添加到您的pom中。xml

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.13.0</version>
    </dependency>
    

    我从here得到

  3. # 3 楼答案

    使用Selenium 3.4.0和;Mozilla Firefox 53。你需要下载最新的geckodriver v0。16.1来自here。将其保存在您的机器中&;在代码中提供geckodriver的绝对路径

    确保已更新pom。具有所需依赖项的xml,如下所示:

    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
    </dependency> 
    

    建议使用WebDriver接口,而不是使用FirefoxDriver实现

    您的代码如下所示:

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();       
    driver.navigate().to("http://www.google.com");
    

    提供以下命令以清除以前的依赖项,安装新的依赖项&;执行测试:

    >mvn clean
    >mvn install
    >mvn test 
    
  4. # 4 楼答案

    我找不到gecko驱动程序的Maven坐标,Selenium 3.4+现在需要gecko驱动程序。可能有人已经创建了一个公共存储库,但下载驱动程序并将其直接添加到项目中仍然很简单。为了避免静态路径问题(将这些驱动程序保留在项目中,以便以后不会中断,并且可以发送整个项目,而不会使设置复杂化),最好将这些驱动程序放在您的projectssrc/main/resources文件夹下

    从以下位置下载驱动程序:https://github.com/mozilla/geckodriver/releases(ARM、Linux、Mac和Windows驱动程序下载)

    如果您使用多个操作系统,您可能需要切换基于操作系统使用的驱动程序:How do I programmatically determine operating system in Java?

    package com.kenmcwilliams.demo;
    
    import java.io.IOException;
    import java.nio.file.FileSystems;
    import java.nio.file.Path;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    /**
     *
     * @author ken
     */
    public class App {
    
        public static void main(String[] args){
            //if you're going to use more than one OS, you should make this switchable based on OS.
            Path path = FileSystems.getDefault().getPath("src/main/resources/geckodriver");
            System.setProperty("webdriver.gecko.driver",path.toString());
            WebDriver driver = new FirefoxDriver();
            //from here down is just a working example...
            driver.get("http://www.google.com");
            WebElement element = driver.findElement(By.name("q"));
            element.sendKeys("Cheese!");
            element.submit();
            System.out.println("Page title is: " + driver.getTitle());
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return d.getTitle().toLowerCase().startsWith("cheese!");
                }
            });
            System.out.println("Page title is: " + driver.getTitle());
            driver.quit();
        }
    }
    
  5. # 5 楼答案

    下载Gecko驱动程序:https://github.com/mozilla/geckodriver/releases

    System.setProperty("webdriver.gecko.driver", "c:\\geckodriver.exe");
    WebDriver driver = new MarionetteDriver();
    driver.get("http://www.google.com");
    
  6. # 6 楼答案

    我也面临着同样的问题,并且一直在寻找解决办法。即使您更改代码或依赖项,您的代码仍然会从错误的位置获取selenium JAR,因为您的代码已经生成,并且分配了错误的selenium JAR

    遵循以下步骤:

    1. 右键单击Eclipse项目上的Maven dependencies,然后单击Configure Maven dependencies并从列表中下拉Maven dependencies,并确定它所在的.m2文件夹
    2. 一旦您确定了.m2文件夹,打开它,转到存储库并转到org文件夹
    3. 在该文件夹中删除所有Selenium文件夹
    4. 返回pom.xml文件,粘贴Selenium 3.4.0依赖项并删除所有3.5.3或其他内容(仅3.4.0依赖项就足够了)。再次删除所有其他selenium依赖项
    5. 最后,保存您的文件并从项目部分构建它,现在您应该可以开始了