Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在后台打开webbrowser_Python_Python 3.x_Macos_Python Webbrowser - Fatal编程技术网

Python 在后台打开webbrowser

Python 在后台打开webbrowser,python,python-3.x,macos,python-webbrowser,Python,Python 3.x,Macos,Python Webbrowser,如何在不更改窗口焦点的情况下在后台使用默认webbrowser打开URL 换句话说,我想在打开webbrowser时留在终端 我尝试了webbrowser模块,但没有成功 Python 3.8.1 (default, Jan 17 2020, 10:45:46) >>> import webbrowser >>> webbrowser.open("https://stackoverflow.com", autoraise=False) 有没有一个简单的方法来

如何在不更改窗口焦点的情况下在后台使用默认webbrowser打开URL

换句话说,我想在打开webbrowser时留在终端

我尝试了
webbrowser
模块,但没有成功

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

有没有一个简单的方法来解决这个问题,或者这是一个Mac OS的问题

您可以尝试以下操作,而不是webbrower模块:

import subprocess

url = subprocess.getoutput("google-chrome-stable https://stackoverflow.com")
url

您可以使用subprocess.check_输出并将terminal命令作为数组传递:

在mac终端打开命令将为您做的工作

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'])

您应该使用selenium并下载chrome驱动程序,并为您的chrome浏览器添加headless选项

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)

谢谢我通过
”/bin/sh:google-chrome-stable:command-not-found得到这个错误。我还希望它使用我的默认webbrowser,因为这可能会发生变化。它确实打开URL,但不会在后台打开。@Farhoud感谢您提供此代码片段,它可能会提供一些有限的、即时的帮助。通过展示为什么这是一个很好的问题解决方案,A将极大地提高它的长期价值,并将使它对未来有其他类似问题的读者更有用。请在你的回答中添加一些解释,包括你所做的假设。非常感谢!带有
--headless
选项的Selenium按预期工作。