【问题标题】:How to start FireFoxDriver using Selenium 3.4.0 using Maven?如何使用 Maven 使用 Selenium 3.4.0 启动 FireFoxDriver?
【发布时间】:2017-10-01 03:55:26
【问题描述】:

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

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

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

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 类型解析为任何依赖项。请帮忙。

【问题讨论】:

    标签: java maven selenium dependencies


    【解决方案1】:

    要使用 Selenium 3.4.0 和 Mozilla Firefox 53.x,您需要从 here 下载最新的 geckodriver v0.16.1。将其保存在您的机器中并在您的代码中提供 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 
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,并且一直在寻找解决方案。即使您更改了代码或依赖项,您的代码仍然会从错误中获取 selenium jar,因为您的代码已经构建并且分配了错误的 selenium jar。

      按照以下步骤操作:

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

      【讨论】:

        【解决方案3】:

        我找不到 Selenium 3.4+ 现在需要的 gecko 驱动程序的 Maven 坐标。有人可能已经创建了一个公共存储库,但下载驱动程序并将它们直接添加到项目中仍然很简单。为避免静态路径问题(将这些驱动程序保留在项目中,以便以后不会中断,并且可以在不复杂设置的情况下发送整个项目)最好将这些驱动程序放在您的项目src/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();
            }
        }
        

        【讨论】:

          【解决方案4】:

          下载 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");
          

          【讨论】:

            【解决方案5】:

            使用以下依赖项下载 selenium。

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

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

            这将解决您的问题

            【讨论】:

              【解决方案6】:

              在你的 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

              【讨论】:

                猜你喜欢
                • 2016-08-19
                • 1970-01-01
                • 2015-02-23
                • 1970-01-01
                • 2019-04-18
                • 1970-01-01
                • 2017-10-24
                • 2018-10-20
                • 1970-01-01
                相关资源
                最近更新 更多