top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Headless Chrome Browser with Selenium



What is Headless Chrome?

A headless is a Non GUI(Graphical User Interface) Version of a Chrome Browser. It is used to run job faster. It is available since chrome version 59 to support headless mode. Available for Linux, MacOS and Windows.

Check your Chrome Browser Version by clicking on 3 dots on the top right side of your browser>Help>About Google Chrome.


when you click on About Google Chrome you can find the version of your chrome Browser.

General syntax to declare ChromeOptions class: 1. ChromeOptions options = new ChromeOptions(); 2. options.addArguments("--headless"); 3. ChromeDriver driver = new ChromeDriver(options);

This is the general syntax for using ChromeOptions.

How to use Headless Chrome?

Using headless browser we can make the job to run faster.

ChromeOptions options = new ChromeOptions();

options.addArguments("--headless");

Here when we run Java application the GUI is not displayed but the application runs and is displayed in the console.

What is Chrome Options?

ChromeOptions class provides some advanced arguments to manipulate selenium chromedriver properties. Below are the lists of operations that are commonly used with ChromeOptions class:

  • Start-maximized: Opens chrome browser in maximized mode

  • Headless: Launch chrome in headless mode i.e. without GUI

  • Incognito: Launch chrome browser in incognito mode

  • Version: Displays/print chrome browser version

  • Disable -extensions: Disable existing chrome extensions

  • Disable-popup-blocking: Disable pop-ups being displayed on chrome

Sample code to check Headless Browser:

package HelloGoogle; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import io.github.bonigarcia.wdm.WebDriverManager; public class HeadlessChromeBrowser { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\rosel\\eclipse-workspace1\\InitatingChromeBrowser\\chromedriver.exe"); WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*");

//Using Headless Browser options.addArguments("--headless"); WebDriver driver = new ChromeDriver(options); driver.get("https://www.Google.com/"); System.out.println(driver.getTitle()); driver.findElement(By.name("q")).sendKeys("Headless Chrome Browser"); driver.findElement(By.xpath("//textarea[@name='q']")).sendKeys(Keys.RETURN); System.out.println(driver.getTitle()); driver.close(); driver.quit(); System.out.println("Google Search done successfully"); } }

Here when i run this program the chrome browser is not displayed in GUI instead it is running in background and its not visible but required actions is displayed in the console. This makes my job faster. Hope this is helpful. :)




319 views1 comment

Recent Posts

See All

1 comentário

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
Convidado:
08 de ago. de 2023
Avaliado com 5 de 5 estrelas.

When it comes to securing sensitive information while browsing the internet, the Utopia P2P web proxy stands out as the ultimate option. With robust encryption protocols and decentralized routing, it guarantees the highest level of security, effectively shielding user data from prying eyes.


Curtir
bottom of page