6

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)
Rob
  • 14,746
  • 28
  • 47
  • 65
user8495738
  • 147
  • 1
  • 3
  • 14

7 Answers7

3

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()
cyberj0g
  • 3,707
  • 1
  • 19
  • 34
  • good answer, only problem is, it doesn't answer the question. This makes firefox hidden not geckodriver. – user890332 Jan 17 '19 at 17:39
  • In my case (Ubuntu 18.10 with Gnome), this argument hides both console and Firefox window. I see that a screenshot in the question suggests Windows, still this answer might be useful for someone. – cyberj0g Jan 18 '19 at 05:57
  • It helped me. Thanks! – user890332 Jan 21 '19 at 15:06
2

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);
user890332
  • 1,315
  • 15
  • 15
1

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

Skandix
  • 1,916
  • 6
  • 27
  • 36
  • Thanks a lot for your help. But is there not another way? Because pyvirtualdisplay is very early stage (0.2.) therefore I am afraid of the bugs. – user8495738 Aug 30 '17 at 07:11
  • not working for me, getting a lot of errors. Also the question was specified on geckodriver.exe display not browser, cause you can hide the browser with options = Options() options.add_argument('--headless') webdriver.Firefox(options=options) – veritaS Nov 03 '18 at 23:45
1

Here the approach which has solved it in my case. I used @thekingofravens suggestion but realized that it is just enough to create the Run.bat file which he mentions in his post. Previously I ran my programms in IDLE with F5. Because of this the Geckodriver popped up every few seconds.

My Solution: I just made the Run.bat file with the same code:

cd C:\PathToYourFileWhichIsCausingTheGeckodriverToPopUp 
python FileWhichIsCausingTheGeckodriverToPopUp.py

And thats it. Just start this file when you want to run your code and the Geckodriver won't pop up. (That it works without the whole path to you program, Python has to be in PATH.) Also, of course you can run your program from the command line with the same commands like above and without creating an extra bat file.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
TT3X20
  • 23
  • 4
0

You have to take care of a couple of stuffs here:

  1. Keep the useful imports. Unusual imports like from selenium import * (in your code) must be avoided.
  2. Keep your code minimal. wait=WebDriverWait(driver,120) have no usage in your code block.
  3. When you use the raw r switch remember to use single quotes '...'
  4. If you initialize the webdriver instance remember to call the quit() method.
  5. Be careful about indentation while using Python.
  6. Here is the minimal code which uses geckodriver to open the url http://www.google.com, print the Page Title and quits the driver instance.

from selenium import webdriver

driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
url = r'http://www.google.com'
driver.get(url)
print("Page Title is : %s" %driver.title)
driver.quit()

Console Output:

Page Title is : Google
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks a lot for your advise, but ^that is just a tiny fraction of my code (1500 lines). I also had to change the website name (company policy). Any idea how to hide the geckodriver? – user8495738 Aug 30 '17 at 13:56
  • @user8495738 Do you see the geckodriver window when you run my code block? – undetected Selenium Aug 30 '17 at 14:05
  • while this contains general useful hints, this doesn't adress the OP's question. He wants to hide the console window. – Skandix Nov 02 '18 at 10:35
0

So I found a very generic workaround to solve this on Windows (in Linux it doesn't seem to be an issue in the first place, can't speak for OSX).

So you need to make three files and its very awkward.

First, you make a file. call it start.bat (or anything) and put the following code in it:

wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\Run.bat"

This will be the top level batch script. It will be visible for a split second while it launches a visual basic script and passes a batch script as an argument. The purpose of this is to make the next console invisible. Next we make the VBscript, invisible.vbs:

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Finally, we make the script that invisible.vbs is supposed to hide, we could call it Run.bat

cd C:\Wherever
python script_using_geckodriver.py

What happens is, as follows:

  1. The first .bat file launches invisible.vbs
  2. invisible.vbs launches the second .bat file without showing it on screen.
  3. The second .bat file then launches the python program. Python (and geckodriver) output to the invisible cmd therefore hiding the geckodriver console window.

P.S. all of this works with PyInstaller to produce a single redistributable package the user can just click on.

Credit harrymc @ superuser.com for this solution, which I found when trying to solve an unrelated problem. I tested and realized it was cross applicable to this. https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way

Raven King
  • 155
  • 1
  • 12
0

So this isn't a total solution, but you can mitigate the geckodriver command window's intrusiveness by making it smaller and/or putting it in a less bothersome place. To do so, open a command window, right-click the top bar, left-click defaults, and go to the Layout tab. From there, you can set the default size-position.

DongKy
  • 113
  • 4