How to start FireFoxDriver using Selenium 3.4.0 us

2019-02-23 02:10发布

问题:

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:-

<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:-

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.

回答1:

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.

Ensure that you have updated the pom.xml with the required dependency as follows:

<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.

Your code will look like:

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

Provide the following commands to flush out the previous dependencies, install the new dependencies & execute your test:

>mvn clean
>mvn install
>mvn test 


回答2:

I am pretty sure that the instanciation of the Firefox driver has changed in Version 3 of Selenium. Please use this code:

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;


回答3:

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.

Follow these steps:

  1. 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.
  2. Once you identify .m2 folder, open it, go to repository and go to org folder.
  3. In that folder delete all of your Selenium folders.
  4. 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.
  5. Finally, save your file and build it from project section and now you should be good to go.


回答4:

Download Gecko Driver: 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:

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.

Download the drivers from: https://github.com/mozilla/geckodriver/releases (ARM, Linux, Mac, and Windows driver downloads)

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?

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();
    }
}


回答6:

use the below dependency to download 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



回答7:

add this in your 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