Rana Haram Rana Haram - 9 days ago 5
Java Question

selenium installation hurdle "importfirefoxdriver"

I'm trying to download selenium web driver using eclipse and I am on the final step and successfully imported web driver, however, when I attempt to do the same for firefox I don't get the import option. Any Suggestions? Is there anything wrong with the code below?

Code:

package webdriver_project;

import org.openqa.selenium.WebDriver;

public class webdriver_module_1 {
public static void main(String[] args) {
WebDriver driver = new firefoxDriver();
}

}

Answer

If you are using Firefox version 48 or later, you must first download Marionete Driver from here: https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

Choose a version suitable for your system (windows/linux, 32 or 64bit), download it and update the Path system variable to add the full directory path to the executable.
See an official information in the changelog: https://github.com/SeleniumHQ/selenium/blob/master/dotnet/CHANGELOG

Geckodriver is now the default mechanism for automating Firefox. This is Mozilla's implementation of a driver for that browser, and is required for automating Firefox versions 48 and above.


I am not sure how do you download selenium using eclipse. Did you download libraries (jars) from their page and place it manually as externall jars in the Eclipse using Java Build Path / Libraries option ?

Anyway, in my opinion the easiest way is to convert the project into Maven project:

  • first install Maven plugin using Eclipse Marketplace option: http://www.eclipse.org/m2e/
  • next right click on the project in Eclipce, and then choose Configure/Convert to Maven project. Next edit pom.xml file and add to it a dependency from Selenium webpage: http://docs.seleniumhq.org/download/maven.jsp

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

The whole content of pom.xml in my example project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>WebKierowca</groupId>
    <artifactId>WebKierowca</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
</project>

Finally create the below java class, change a path pointing to Marionette driver (geckodriver.exe), right click on this class and run it as Java application. If everything is ok, it should lauch Firefox, go to google webpage, search a word "selenium" and display search results for 5 seconds:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {

    public static void main(String ... x){
        // Path to Marionette driver
        System.setProperty("webdriver.gecko.driver", "C:/serwery/geckodriver.exe");

        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.google.com");

        driver.findElement(By.name("q")).sendKeys("Selenium");
        driver.findElement(By.name("btnG")).click();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        driver.quit();
    }
}
Comments