我遇到了类似的问题。我刚刚使用 os.system() 打开了一个新的 chrome 实例。我还使用以下方法将 chrome.exe 注册为新浏览器:
#register the browser
chrome_path = "C:\\Users\\cj9250\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
其他帖子中有关于使用 .get 调用 chrome 路径的指南。尽管尝试了这种方法和许多其他不同的方法,但我还是无法让 chrome 打开一个新窗口和会话。
最后我来到os.system打开一个新的chrome实例。因此,对于您的代码,它看起来像这样:
import webbrowser
import os #use for new instance of chrome
#urls I want to open in array
URLS = (
"first page",
"second page",
"third page"
)
#register the browser
chrome_path = "YOUR PATH TO CHROME\\chrome.exe" #note the double \ syntax
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
#open new instance of chrome
os.system(r'YOUR PATH TO CHROME\chrome.exe')
#open each url
for url in URLS:
webbrowser.get('chrome').open(url)
看看你想要做什么,我认为 Selenium 会更适合。我也每天在工作中使用 Web 应用程序,并且花时间浏览了一些 Selenium 教程。自动化要简单得多。