Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium : How to hide geckodriver?

I am writing an program for a web automation in python. Is here a ways to hide the geckodriver? So that the console (see picture) won't show up when I start the program.

console of geckodriver

here is a fraction of my code:

from selenium import webdriver
from selenium import *
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC`


driver=webdriver.Firefox()
wait=WebDriverWait(driver,120)
url = r"http://google.com"
driver.get(url) #This line starts the console (see picture)
like image 665
user8495738 Avatar asked Aug 30 '17 05:08

user8495738


3 Answers

To prevent geckodriver from displaying any windows, you need to pass -headless argument:

from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
driver.get('https://www.example.com')
...
driver.quit()
like image 127
cyberj0g Avatar answered Sep 23 '22 19:09

cyberj0g


This worked for me in C#. It blocks both geckodriver and firefox window

                FirefoxOptions f = new FirefoxOptions();
                f.AddArgument("-headless");                    
                var ffds = FirefoxDriverService.CreateDefaultService();
                ffds.HideCommandPromptWindow = true;
                driver = new FirefoxDriver(ffds,f);
like image 36
user890332 Avatar answered Sep 24 '22 19:09

user890332


I was able to do that after implementing PyVirtualDisplay

sudo pip install pyvirtualdisplay # Install it into your Virtual Environment

Then just import Display as follows:

from pyvirtualdisplay import Display

Then, before fetching, start the virtual display as follows:

# initiate virtual display with 'visible=0' activated
# this way you will hide the browser
display = Display(visible=0, size=(800, 600))
# Start Display
display.start()

...
# Do your fetching/scrapping
...

# Stop Display
display.stop()

I hope it helps

like image 33
Saul Bretado Avatar answered Sep 21 '22 19:09

Saul Bretado