Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Google Chrome using Python and pass in arguments?

Here is how I am trying to do it:

# Start Google Chrome
subprocess.call(["C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "--kiosk"])

If I add the --kiosk flag to the Google Chrome shortcut on my desktop, Chrome does start in kiosk mode. However, when I try this through Python, it doesn't seem to work. I've searched Google and here, but have found nothing so far. Please help.

like image 287
Beebunny Avatar asked Dec 14 '22 23:12

Beebunny


2 Answers

That command works for me just fine.

Make sure you're not running another copy of Chrome. It appears that Chrome will only start in Kiosk mode if no other instances are running. If you want to make sure no other instances are running, this answer shows how you could kill them before starting a new process:

import os
import subprocess


CHROME = os.path.join('C:\\', 'Program Files (x86)', 'Google', 'Chrome', 'Application', 'chrome.exe')

os.system('taskkill /im chrome.exe')
subprocess.call([CHROME, '--kiosk'])

As a side note, it is always nice to use os.path.join, even if your code is platform-specific at this point.

like image 94
Attila O. Avatar answered Jan 13 '23 14:01

Attila O.


You could use raw-string literals for Windows paths:

import subprocess

chrome = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
subprocess.check_call([chrome, '--kiosk'])

Note: "\\n" == r'\n' != '\n'. Though it doesn't make any difference in your case.

You could try to pass --new-window option to open a new window.

If all you need is to open an url in a new Google Chrome window:

import webbrowser

webbrowser.get('google-chrome').open_new('https://example.com')
like image 43
jfs Avatar answered Jan 13 '23 14:01

jfs