How to use the gecko executable with Selenium

前端 未结 10 1447
滥情空心
滥情空心 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 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:

    
        io.github.bonigarcia
        webdrivermanager
        4.2.2
    
    

    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.

提交回复
热议问题