Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start FireFoxDriver using Selenium 3.4.0 using Maven?

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.

like image 911
Bharat Nanwani Avatar asked May 03 '17 10:05

Bharat Nanwani


3 Answers

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.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 
like image 151
undetected Selenium Avatar answered Sep 18 '22 20:09

undetected Selenium


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.
like image 38
ANVESH POLURI Avatar answered Sep 17 '22 20:09

ANVESH POLURI


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();
    }
}
like image 20
Quaternion Avatar answered Sep 19 '22 20:09

Quaternion