How to run selenium (Firefox) web driver without a GUI?



We can run Selenium (Firefox) webdriver without a GUI. This means that the execution has to be kicked in headless mode. The headless execution is popular now since it results in less consumption of resources.

Firefox, without GUI, can be executed after we set the geckodriver path. We shall take the help of FirefoxOptions class, share this information to the browser via the setHeadless method. Finally pass true as a parameter to that.

Syntax

FirefoxOptions op = new FirefoxOptions(); op.setHeadless(true); WebDriver driver = new FirefoxDriver(op);

Example

Code Implementation.

import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import java.util.concurrent.TimeUnit; public class FirefoxNoGUI{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");       //FirefoxOptions object       FirefoxOptions op = new FirefoxOptions();       //pass true to headless mode       op.setHeadless(true);       // send headless information to Firefox driver       WebDriver driver = new FirefoxDriver(op);       // wait of 5 seconds       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);       driver.get("https://www.tutorialspoint.com/about/about_careers.htm");       // get page title       System.out.println("Page Title without GUI: " + driver.getTitle());       driver.quit();    } }

Output

Updated on: 2020-12-28T13:29:01+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements