Search code examples
pythonpython-3.xmacospython-webbrowser

Open webbrowser in background


How can I open a URL with my default webbrowser in the background without changing window focus?

In other words I want to stay in the terminal while opening the webbrowser.

I have tried the webbrowser module without success.

Python 3.8.1 (default, Jan 17 2020, 10:45:46)
>>> import webbrowser
>>> webbrowser.open("https://stackoverflow.com", autoraise=False)

Is there an easy way to solve this or is it a Mac OS problem?


Solution

  • You should use selenium and download chrome driver instead and add headless option for your chrome browser.

    from selenium import webdriver
    import time
    
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--disable-notifications')
    chrome_options.add_argument("--headless")
    driver = webdriver.Chrome(executable_path="/Users/mypc/Downloads/chromedriver-2",options=chrome_options)
    
    # or you can use Chrome(executable_path="/usr/bin/chromedriver")
    
    driver.get("https://www.instagram.com/accounts/login/")
    time.sleep(2)