Invoke Firefox browser using Selenium 4 and JAVA

Versions used while doing this blog post, (These are the prerequisites to automate firefox browser. Browser and driver versions should match)
selenium-java version – 4.4.0
firefox browser version – 105
gecko driver version – 0.31.0 (Download link : https://github.com/mozilla/geckodriver/releases) [using WebDriverManager to auto download the version]

Firefox browser can be opened using selenium by following below steps:
1. Map the correct version of gecko driver using WebDriverManager,

WebDriverManager.firefoxdriver().driverVersion("0.31.0").setup();

2. Instantiate (creation of object) driver object with FirefoxDriver() class (This step opens the firefox browser)

WebDriver driver = new FirefoxDriver();

3. Now, next steps of maximizing the browser, setting implicit timeout for driver object and launch of expected website can be carried out

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.manage().window().maximize();
driver.get("https://www.google.com");
driver.findElement(By.name("q")).sendKeys("Test");

4. Quit/close the driver object after automating the necessary flows within the website.

driver.quit();

Sample code for reference

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.time.Duration;

public class OpenFirefoxUsingSelenium4 {

    public static void main(String[] args){
        WebDriverManager.firefoxdriver().driverVersion("0.31.0").setup();
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
        driver.findElement(By.name("q")).sendKeys("Test");
        driver.quit();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *