Python Selenium : How to hide geckodriver?
Asked Answered
M

7

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)
Marylinmarylinda answered 30/8, 2017 at 5:28 Comment(9)
where do u want to hide it? in the code?Unmannerly
Thanks for your response. I just want to prevent the pop up (console) of the geckodriver.exe to appear when I start the program.Marylinmarylinda
well, as far as I have done it I haven't noticed such thing. just place the geckodriver.exe to the folder location of the project. Another way is to use it in code.Unmannerly
I just add a picture to make things more clearMarylinmarylinda
which language are you using? You never even mentioned thatSofthearted
Aren't you writing your python code within an IDE? Can you share your code block? If you are starting gecko as a service it is bound to show up.Brassica
Here is my codeMarylinmarylinda
I write the program in pythonMarylinmarylinda
how did you resolve this?Baptlsta
S
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()
Saccharo answered 10/1, 2019 at 11:6 Comment(3)
good answer, only problem is, it doesn't answer the question. This makes firefox hidden not geckodriver.Babbage
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.Saccharo
It helped me. Thanks!Babbage
B
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);
Babbage answered 21/1, 2019 at 15:4 Comment(1)
This helps me. But I needed the python way too!Exuberant
D
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

Daedal answered 30/8, 2017 at 6:2 Comment(2)
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.Marylinmarylinda
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)Baptlsta
B
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.

Bevis answered 4/2, 2019 at 13:27 Comment(1)
thanks it works! geckodriver.exe's shell is hidden, but now i have cmd's shell upExtrude
B
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
Brassica answered 30/8, 2017 at 8:47 Comment(3)
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?Marylinmarylinda
@Marylinmarylinda Do you see the geckodriver window when you run my code block?Brassica
while this contains general useful hints, this doesn't adress the OP's question. He wants to hide the console window.Realm
K
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

Karrikarrie answered 23/1, 2019 at 16:15 Comment(0)
M
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.

Magnifico answered 24/3, 2023 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.