How to use the gecko executable with Selenium

前端 未结 10 1412
滥情空心
滥情空心 2020-11-28 08:20

I\'m using Firefox 47.0 with Selenium 2.53. Recently they have been a bug between Selenium and Firefox which make code not working. One of the solution is to use the Marion

相关标签:
10条回答
  • 2020-11-28 08:56

    Recently Selenium has launched Selenium 3 and if you are trying to use Firefox latest version then you have to use GeckoDriver:

    System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    

    You can check full documentation from here

    0 讨论(0)
  • 2020-11-28 08:57

    It is important to remember that the driver(file) must have execution permission (linux chmod +x geckodriver).

    To sum up:

    1. Download gecko driver
    2. Add execution permission
    3. Add system property:

      System.setProperty("webdriver.gecko.driver", "FILE PATH");

    4. Instantiate and use the class

      WebDriver driver = new FirefoxDriver();

    5. Do whatever you want

    6. Close the driver

      driver.close;

    0 讨论(0)
  • 2020-11-28 08:58

    I try to make it simple. You have two options while using Selenium 3+:

    • Either upgrade your Firefox to 47.0.1 or higher and use the default geckodriver of Selenium3.

    • Or disable using of geckodriver by specifying marionette to false and use the legacy Firefox driver. a simple command to run selenium is: java -Dwebdriver.firefox.marionette=false -jar selenium-server-standalone-3.0.1.jar. You can also disable using geckodriver from other commands that are mentioned in other answers.

    0 讨论(0)
  • 2020-11-28 09:06

    I create a simple Java application by archetype maven-archetype-quickstar, then revise pom.xml:

    <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>com.example</groupId>
        <artifactId>bar</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>bar</name>
        <description>bar</description>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.0.0-beta3</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-server</artifactId>
                <version>3.0.0-beta3</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-api</artifactId>
                <version>3.0.0-beta3</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-firefox-driver</artifactId>
                <version>3.0.0-beta3</version>
            </dependency>
        </dependencies>
        <build>
            <finalName>bar</finalName>
        </build>
    </project>
    

    and

    package bar;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class AppTest {
    
        /**
         * Web driver.
         */
        private static WebDriver driver = null;
    
        /**
         * Entry point.
         * 
         * @param args
         * @throws InterruptedException
         */
        public static void main(String[] args) throws InterruptedException {
            // Download "geckodriver.exe" from https://github.com/mozilla/geckodriver/releases
            System.setProperty("webdriver.gecko.driver","F:\\geckodriver.exe");
            driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://localhost:8080/foo/");
            String sTitle = driver.getTitle();
            System.out.println(sTitle);
        }
    
    }
    

    You also use on Mac OS X, Linux: https://github.com/mozilla/geckodriver/releases

    and

    // On Mac OS X.
    System.setProperty("webdriver.gecko.driver", "/Users/donhuvy/Downloads/geckodriver");
    
    0 讨论(0)
  • 2020-11-28 09:07

    I am also facing the same issue and got the resolution after a day :

    The exception is coming because System needs Geckodriver to run the Selenium test case. You can try this code under the main Method in Java

        System.setProperty("webdriver.gecko.driver","path of/geckodriver.exe");
        DesiredCapabilities capabilities=DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        WebDriver driver = new FirefoxDriver(capabilities);
    

    For more information You can go to this https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver link.

    Please let me know if the issue doesn't get resolved.

    0 讨论(0)
  • 2020-11-28 09:11

    You can handle the Firefox driver automatically using WebDriverManager.

    This library downloads the proper binary (geckodriver) for your platform (Mac, Windows, Linux) and then exports the proper value of the required Java environment variable (webdriver.gecko.driver).

    Take a look at a complete example as a JUnit test case:

    public class FirefoxTest {
    
      private WebDriver driver;
    
      @BeforeClass
      public static void setupClass() {
        WebDriverManager.firefoxdriver().setup();
      }
    
      @Before
      public void setupTest() {
        driver = new FirefoxDriver();
      }
    
      @After
      public void teardown() {
        if (driver != null) {
          driver.quit();
        }
      }
    
      @Test
      public void test() {
        // Your test code here
      }
    }
    

    If you are using Maven you have to put at your pom.xml:

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>4.2.2</version>
    </dependency>
    

    WebDriverManager does magic for you:

    1. It checks for the latest version of the WebDriver binary
    2. It downloads the WebDriver binary if it's not present on your system
    3. It exports the required WebDriver Java environment variables needed by Selenium

    So far, WebDriverManager supports Chrome, Opera, Internet Explorer, Microsoft Edge, PhantomJS, and Firefox.

    0 讨论(0)
提交回复
热议问题