您的位置:首頁技術文章
文章詳情頁

Python:chrome.exe的通用webbrowser.get()。open()無法正常工作

瀏覽:64日期:2022-08-07 14:04:15
如何解決Python:chrome.exe的通用webbrowser.get()。open()無法正常工作?

您必須在webbrowser.get調用中使用unix樣式的路徑:

webbrowser.get('C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s').open('http://google.com')

這是因為webbrowser內部shlex.split在路徑上執行,只會刪除Windows樣式的路徑分隔符:

>>> cmd = 'C:Usersoreild1AppDataLocalGoogleChromeApplicationchrome.exe %s'>>> shlex.split(cmd)[’C:Usersoreild1AppDataLocalGoogleChromeApplicationchrome.exe’, ’%s’]>>> cmd = 'C:/Users/dan/AppData/Local/Google/Chrome/Application/chrome.exe %s'>>> shlex.split(cmd)[’C:/Users/dan/AppData/Local/Google/Chrome/Application/chrome.exe’, ’%s’]

shlex如果給定posix=False@R_168_5301@,實際上webbrowser將在這里做正確的事情,但即使在Windows上也不會提供。可以說這是一個錯誤webbrowser。

解決方法

我使用的是Python 2.7(Win 8.1x64),我想在Chrome中打開一個URL。由于Chrome僅在3.3+中受本機支持,因此我嘗試了一個通用調用:

import webbrowserwebbrowser.get('C:Program Files (x86)GoogleChromeApplicationchrome.exe %s').open('http://google.com')

路徑是正確的,并且print確實為我提供了處理程序:

'<webbrowser.GenericBrowser object at 0x0000000002D26518>'

但是,open()-最好是open_new_tab())-功能不起作用。它返回False。

如果我運行命令

'C:Program Files (x86)GoogleChromeApplicationchrome.exe' 'https://google.com'

在Windows運行對話框中,它確實起作用。

如果我將Chrome設置為標準瀏覽器并運行

webbrowser.get().open('http://google.com')

它確實有效,但這不是我想要的。

有誰知道出什么事了嗎?

標簽: Python 編程
相關文章:
国产综合久久一区二区三区