9

This works, in powershell:

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

How can this be achieved from Python?

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
3z33etm
  • 1,083
  • 3
  • 15
  • 23

5 Answers5

19

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)
Vivek Kumar
  • 191
  • 1
  • 3
5

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.

nshoo
  • 939
  • 8
  • 17
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.

intboolstring
  • 6,891
  • 5
  • 30
  • 44
  • '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! – 3z33etm Jun 11 '16 at 23:14
2
import subprocess
subprocess.Popen(["C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "-incognito", "www.google.com"])
3z33etm
  • 1,083
  • 3
  • 15
  • 23
-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)