Java 如何使用 Maven 使用 Selenium 3.4.0 启动 FireFoxDriver?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43757984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start FireFoxDriver using Selenium 3.4.0 using Maven?
提问by Bharat Nanwani
I am trying to use Selenium's latest version 3.4.0 in a maven project. I imported all Selenium's jars using below dependency:-
我正在尝试在 Maven 项目中使用 Selenium 的最新版本 3.4.0。我使用以下依赖项导入了所有 Selenium 的 jars:-
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
The problem is I am unable to resolve any dependency in my project in Eclipse for below code inside main method:-
问题是我无法在 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");
}
}
What am I missing? Eclipse is unable to resolve FirefoxDriver type to any dependencies. Please help.
我错过了什么?Eclipse 无法将 FirefoxDriver 类型解析为任何依赖项。请帮忙。
回答by JDTLH9
I am pretty sure that the instanciation of the Firefox driver has changed in Version 3 of Selenium. Please use this code:
我很确定 Firefox 驱动程序的实例化在 Selenium 版本 3 中发生了变化。请使用此代码:
System.setProperty("webdriver.firefox.driver","C:\Program Files (x86)\Mozilla Firefox\firefox.exe");
WebDriver driver = new FirefoxDriver();
Please also read more about this here
另请在此处阅读有关此内容的更多信息
You will also find working test code here
您还可以在此处找到有效的测试代码
Also please check that you have included the correct import statements at the top of your class:
另请检查您是否在类的顶部包含了正确的导入语句:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
回答by DebanjanB
To work with Selenium 3.4.0 & Mozilla Firefox 53.x you need to download the latest geckodriver v0.16.1 from here. Save it in your machine & provide absolute path of the geckodriver in your code.
要使用 Selenium 3.4.0 和 Mozilla Firefox 53.x,您需要从这里下载最新的 geckodriver v0.16.1 。将其保存在您的机器中并在您的代码中提供 geckodriver 的绝对路径。
Ensure that you have updated the pom.xml with the required dependency as follows:
确保您已使用所需的依赖项更新 pom.xml,如下所示:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
It is recommended to use the WebDriver
interface rather than to use the FirefoxDriver
implementation.
建议使用WebDriver
接口而不是使用FirefoxDriver
实现。
Your code will look like:
您的代码将如下所示:
System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.google.com");
Provide the following commands to flush out the previous dependencies, install the new dependencies & execute your test:
提供以下命令以清除以前的依赖项,安装新的依赖项并执行您的测试:
>mvn clean
>mvn install
>mvn test
回答by Roberto
Download Gecko Driver: https://github.com/mozilla/geckodriver/releases
下载 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");
回答by Quaternion
I couldn't find Maven coordinates for the gecko driver which is now required for Selenium 3.4+. Someone has probably created a public repository but it is still simple to download the drivers and add them directly to the project. To avoid static path issues (keeping these drivers with the project so things don't break later and the whole project can be sent without complicating set up) it is best to place these drivers under your projects src/main/resources
folder.
我找不到现在 Selenium 3.4+ 所需的 gecko 驱动程序的 Maven 坐标。有人可能已经创建了一个公共存储库,但下载驱动程序并将它们直接添加到项目中仍然很简单。为了避免静态路径问题(将这些驱动程序保留在项目中,以便以后不会出现问题,并且可以在不复杂设置的情况下发送整个项目)最好将这些驱动程序放在您的项目src/main/resources
文件夹下。
Download the drivers from: https://github.com/mozilla/geckodriver/releases(ARM, Linux, Mac, and Windows driver downloads)
从以下位置下载驱动程序:https: //github.com/mozilla/geckodriver/releases(ARM、Linux、Mac 和 Windows 驱动程序下载)
If you're working with multiple OS's you might want to switch which driver is used based on OS: How do I programmatically determine operating system in Java?
如果您使用多个操作系统,您可能希望根据操作系统切换使用哪个驱动程序:如何以编程方式确定 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();
}
}
回答by jyothish kumar
use the below dependency to download selenium .
使用以下依赖项下载 selenium 。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
Once the dependecies has been downloaded. Perform Build Project.
下载依赖项后。执行构建项目。
This will resolve your issue
这将解决您的问题
回答by ANVESH POLURI
I faced same problem and been searching for solution for a long time. Even if you change code or dependencies, your code will still take selenium jars from wrong, because your code was already built and wrong selenium jars are assigned.
我遇到了同样的问题,并一直在寻找解决方案很长时间。即使您更改代码或依赖项,您的代码仍然会从错误中获取 selenium jar,因为您的代码已经构建并且分配了错误的 selenium jar。
Follow these steps:
按着这些次序:
- Right click on Maven dependencies on your Eclipse project and click Configure Maven Dependencies and drop down Maven dependencies from your list and identify your
.m2
folder where it is located. - Once you identify
.m2
folder, open it, go to repository and go to org folder. - In that folder delete all of your Selenium folders.
- Go back to your
pom.xml
file, paste Selenium 3.4.0 dependency and delete all of your 3.5.3 or other things (only 3.4.0 dependency is more than enough). Once again remove all other selenium dependencies. - Finally, save your file and build it from project section and now you should be good to go.
- 右键单击 Eclipse 项目上的 Maven 依赖项,然后单击配置 Maven 依赖项并从列表中下拉 Maven 依赖项并确定
.m2
它所在的文件夹。 - 确定
.m2
文件夹后,打开它,转到存储库并转到 org 文件夹。 - 在该文件夹中删除所有 Selenium 文件夹。
- 返回您的
pom.xml
文件,粘贴 Selenium 3.4.0 依赖项并删除所有 3.5.3 或其他内容(仅 3.4.0 依赖项绰绰有余)。再次删除所有其他硒依赖项。 - 最后,保存您的文件并从项目部分构建它,现在您应该可以开始了。
回答by ANVESH POLURI
add this in your pom.xml
在你的 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>
iam get from here
我从这里得到