Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide command prompt in Selenium ChromeDriver

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new ChromeDriver(driverService, new ChromeOptions());

Is it possible to achieve this in Python? I've tried all the possible fixes posted here on StackoverFlow and outside, but nothing when I run my .exe, the cmd appears too.

Python 3.6, latest Selenium version (3.9).

like image 317
masc Avatar asked Feb 07 '18 01:02

masc


People also ask

How do I disable Chrome drivers command line?

You can reboot, then delete the studio folder and the project folder, or easier still, run the following command from the command line, depending on which browser's you've executed things in: Chrome: taskkill -F -IM chromedriver.exe Firefox taskkill -F -IM geckodriver.exe Edge taskkill -F -IM MicrosoftWebDriver.exe IE ...

What is WebDriver ChromeOptions ()?

The Chromeoptions Class is a concept in Selenium WebDriver for manipulating various properties of the Chrome driver. The Chrome options class is generally used in conjunction with Desired Capabilities for customizing Chrome driver sessions.

How do I run ChromeDriver in headless mode?

ChromeOptions options = new ChromeOptions() options. addArgument("headless"); ChromeDriver driver = new ChromeDriver(options); In the above code, the browser is instructed to run in the headless mode using the addArgument() method of the ChromeOptions class provided by the Selenium WebDriver.

How can we set the system property for ChromeDriver?

Any of these steps should do the trick: include the ChromeDriver location in your PATH environment variable. (Java only) specify its location via the webdriver.chrome.driver system property (see sample below) (Python only) include the path to ChromeDriver when instantiating webdriver.Chrome (see sample below)


1 Answers

I found the way to replicate the above code in python (more or less) Used this fix as base to my inspiration.

After hours of struggling (and also really bad words), I've made this commit on github bywhich console prompt behaviour is now easily changable via code. I will try to make it available in the official sources. Hope it will make you save time and patience.

STEP 1

Locate service.py, generally in "X:\YourPythonFold\Lib\site-packages\selenium\webdriver\common\service.py"

STEP 2

Replace these lines (n° 72-76 approximately, below start method def):

self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE)

with

if any("hide_console" in arg for arg in self.command_line_args()):
                self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, creationflags=0x08000000)
            else:
                self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() != 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE)

Finally in your code, when you setup your driver (I chose Chrome as example):

args = ["hide_console", ]
driver = webdriver.Chrome("your-path-to-chromedriver.exe", service_args=args, ...)

When edit the source code, be careful to PEP! Do not use tabs, just spaces!

like image 137
masc Avatar answered Sep 28 '22 10:09

masc