Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I'm working on a python script to web-scrape and have gone down the path of using Chromedriver as one of the packages. I would like this to operate in the background without any pop-up windows. I'm using the option 'headless' on chromedriver and it seems to do the job in terms of not showing the browser window, however, I still see the .exe file running. See the screenshot of what I'm talking about. Screenshot

This is the code I am using to initiate ChromeDriver:

options = webdriver.ChromeOptions()

options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])

options.add_argument('headless')

options.add_argument('window-size=0x0')

chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"

Things I've tried to do is alter the window size in the options to 0x0 but I'm not sure that did anything as the .exe file still popped up.

Any ideas of how I can do this?

I am using Python 2.7 FYI

1 Answer

0 votes
by (62.9k points)

To run WebDriver tests in headless mode using ChromeDriver, you will need to add the relevant dependencies in your pom.xml file:

<dependency>

    <groupId>org.seleniumhq.selenium</groupId>

    <artifactId>selenium-chrome-driver</artifactId>

    <version>${selenium.version}</version>

</dependency>

<dependency>

    <groupId>org.seleniumhq.selenium</groupId>

    <artifactId>selenium-server</artifactId>

    <version>${selenium.version}</version>

</dependency>

<dependency>

    <groupId>org.seleniumhq.selenium</groupId>

    <artifactId>selenium-java</artifactId>

    <version>${selenium.version}</version>

</dependency>

<dependency>

    <groupId>io.github.bonigarcia</groupId>

    <artifactId>webdrivermanager</artifactId>

    <version>${webdrivermanager.version}</version>

</dependency>

Now, we instruct the WebDriver to launch ChromeDriver in headless mode:

package com.sdetworld.tutorials.selenium

import io.githib.bonigarcia.wdm.ChromeDriverManager;

import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverBase{

      static protected WebDriver driver;

      public static void setup(){

                 ChromeDriverManager.geInstance.setup();

                 ChromeOptions chromeoptions = new ChromeOptions();

                 

                 chromeoptions.addArguments(“--headless”);

                 driver = new ChromeDriver(chromeOptions);

  }

public static void main(String[] args){

WebDriverBase.setup();

driver.get(“https://www.intellipaat.com/”);

}

} 

Browse Categories

...