how to open chrome in incognito mode from Python
Asked Answered
H

7

9

This works, in powershell:

Start-Process chrome.exe -ArgumentList @( '-incognito', 'www.foo.com' )

How can this be achieved from Python?

Hyperactive answered 11/6, 2016 at 22:48 Comment(0)
D
3

Use the os module to execute the command.

import os
os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -ArgumentList @( '-incognito', 'www.foo.com'" )

More information on os.system can be found here.

Damnatory answered 11/6, 2016 at 22:51 Comment(1)
'Start-Process' is not recognized as an internal or external command, operable program or batch file. Anyway, I found an answer, updating OP now. Thanks!Hyperactive
A
21

Python Script to open incognito mode in chrome using webbrowser

import webbrowser
url = 'www.google.com'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s --incognito'
webbrowser.get(chrome_path).open_new(url)
Anceline answered 20/3, 2017 at 20:7 Comment(0)
H
6

On my computer intboolstring's approach does not work and an alternative and more feature-full approach would be to use call() from the subprocess module though it is still possible with system() if the command is changed.

from subprocess import call
call("\"C:\Path\To\chrome.exe\" -incognito www.foo.com", shell=True)

Or with system():

from os import system
system("\"C:\Path\To\chrome.exe\" -incognito www.foo.com")

It is also possible to start chrome using only "chrome.exe -incognito www.foo.com" if chrome is added to path or by running a command through powershell like so:

system("powershell -C Start-Process chrome.exe -ArgumentList @( '-incognito', 'www.foo.com' )")

Though this method is much slower than adding chrome.exe to path.

Hermitage answered 11/6, 2016 at 23:16 Comment(0)
D
3

Use the os module to execute the command.

import os
os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -ArgumentList @( '-incognito', 'www.foo.com'" )

More information on os.system can be found here.

Damnatory answered 11/6, 2016 at 22:51 Comment(1)
'Start-Process' is not recognized as an internal or external command, operable program or batch file. Anyway, I found an answer, updating OP now. Thanks!Hyperactive
H
2
import subprocess
subprocess.Popen(["C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "-incognito", "www.google.com"])
Hyperactive answered 11/6, 2016 at 23:16 Comment(0)
S
0

The post is quite old but I'd like to share the solution I found after reading the webbrowser.py code (the module is good but the documentation is really too vague).

import webbrowser
webbrowser.register("browser", None, webbrowser.GenericBrowser(["\\full\\path\\to\\chrome.exe", "-incognito", "%s"]), preferred=True)
webbrowser.open(url)

It can be adapted to any brower by changing the browser's path and the option name.

Strand answered 11/9, 2023 at 18:24 Comment(0)
A
0
import os
os.system('open -na  "browser name" —args -incognito "https://www.google.com"')
Abuttals answered 17/12, 2023 at 20:38 Comment(6)
"browser name" opens Chrome? Which part enables incognito mode?Aircraft
for the browser name if you’re using chrome type in “Chrome” if it’s vivaldi then put “Vivaldi” in that “ “ you’ll also need to add -incognito to open up in incognito so you will have to add it in. Ps. So far Arc browser, SigmaOs does not work from my test.Abuttals
That's what the question is asking... put it in your answer instead of making people guess.Aircraft
I dont know how this is making people guess, when i wrote it in the comments and showed what goes where yesterday, but okay lol.Abuttals
Good answers address the question asked (preferably with explanation when code is used), and important information should not go in comments. Comments should be treated as temporary and as they may be deleted at any time without warning. In this case, chrome would be better than browser name because the question asks about chrome. In addition, because this question has been answered multiple times already, it would be good to explain why this answer should be used instead of others here.Aircraft
Is this Mac OSX only? There is no open command on WindowsChavis
L
-1

this code works. it starts a new incognito tab and then switches the driver to control the new tab

def incognito():
    global driver
    driver = webdriver.Chrome()
    driver.get('https://www.google.com')
    search=driver.find_element_by_id('lst-ib')
    incognito=search.send_keys(Keys.CONTROL+Keys.SHIFT+'N')
    driver.switch_to_window(driver.window_handles[-1])
    driver.get('https://web.whatsapp.com/')
    time.sleep(5)
Lobworm answered 27/1, 2018 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.