如何使用Maven使用Selenium 3.4.0启动FireFoxDriver?
我正在尝试在maven项目中使用Selenium的最新版本3.4.0。 我使用以下依赖项导入了所有Selenium的jar子: –
org.seleniumhq.selenium selenium-java 3.4.0
问题是我无法解决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类型解析为任何依赖项。 请帮忙。
要使用Selenium 3.4.0和Mozilla Firefox 53.x,您需要从此处下载最新的geckodriver v0.16.1。 将其保存在您的机器中,并在代码中提供geckodriver的绝对路径。
确保已使用所需的依赖关系更新了pom.xml,如下所示:
org.seleniumhq.selenium selenium-java 3.4.0
建议使用WebDriver
接口而不是使用FirefoxDriver
实现。
您的代码将如下所示:
System.out.println("Welcome to Maven World"); System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); driver.navigate().to("http://www.google.com");
提供以下命令以清除以前的依赖项,安装新的依赖项并执行测试:
>mvn clean >mvn install >mvn test
我遇到了同样的问题,并且一直在寻找解决方案。 即使您更改了代码或依赖项,您的代码仍然会从错误中获取selenium jar,因为您的代码已经构建并且分配了错误的selenium jar。
跟着这些步骤:
- 右键单击Eclipse项目上的Maven依赖项,然后单击“配置Maven依赖项”并从列表中删除Maven依赖项,并标识它所在的
.m2
文件夹。 - 一旦识别出
.m2
文件夹,打开它,转到存储库并转到org文件夹。 - 在该文件夹中删除所有Selenium文件夹。
- 回到你的
pom.xml
文件,粘贴Selenium 3.4.0依赖并删除你所有的3.5.3或其他东西(只有3.4.0的依赖性绰绰有余)。 再次删除所有其他selenium依赖项。 - 最后,保存您的文件并从项目部分构建它,现在您应该好好去。
下载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");
我非常确定Firefox驱动程序的实例在Selenium的第3版中已经发生了变化。 请使用此代码:
System.setProperty("webdriver.firefox.driver","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver();
请在这里阅读更多相关信息
您还可以在此处找到工作测试代码
另外,请检查您是否在课程顶部包含了正确的导入语句:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;
我找不到Selenium 3.4+现在需要的壁虎驱动程序的Maven坐标。 有人可能已经创建了一个公共存储库,但是下载驱动程序并将它们直接添加到项目中仍然很简单。 为了避免静态路径问题(将这些驱动程序与项目保持在一起,以便以后不会中断,整个项目可以在不复杂的情况下发送),最好将这些驱动程序放在项目src/main/resources
文件夹下。
从以下url下载驱动程序: https : //github.com/mozilla/geckodriver/releases (ARM,Linux,Mac和Windows驱动程序下载)
如果您正在使用多个操作系统,则可能需要根据操作系统切换使用的驱动程序: 如何以编程方式确定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() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }); System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }
使用以下依赖项下载selenium。
org.seleniumhq.selenium selenium-java 3.4.0
一旦下载了dependecies。 执行构建项目。
这将解决您的问题
在你的pom.xml中添加它
org.seleniumhq.selenium selenium-firefox-driver 3.13.0
我从这里开始
- 时间延迟和JInput
- 沙盒JVM可以保护服务器不受信任的来源
- Java运行时环境检测到致命错误:pc = 0x00002b2f7e9b2744处的SIGSEGV(0xb),pid = 28778,tid = 1138739520
- Collections.binarySearch如何工作?
- 通过XSSFRichTexString和Jsoup进行Apache POI Excel文本格式化
- Http客户端在java中发布xml文件
- 如何在Executor服务中执行长时间执行任务(例如,无限循环)
- C#中LinkedHashSet(Java)的等价物是什么?
- TestNG + Mockito + PowerMock – verifyStatic()不起作用