Reputation: 558
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?
Upvotes: 2
Views: 7368
Reputation: 147
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)
Upvotes: 1
Reputation: 91
You can use subprocess.check_output and pass terminal command as an array:
in mac terminal open command will do the work for you
import subprocess
#subprocess.check_output(['ls','-l']) #all that is technically needed...
#in open documention in terminal you can use --hide to run in background
print subprocess.check_output(['open','http://google.com/','--hide'])
Upvotes: 0
Reputation: 577
instead of webbrower module you can try:
import subprocess
url = subprocess.getoutput("google-chrome-stable https://stackoverflow.com")
url
Upvotes: 1