我在 Windows 10 上,我正在嘗試用 Chrome 打開一個檔案,但是,PS 總是最終將它發送到相對路徑。
我在一個名為 的目錄中運行命令phy,這是它的結構(相關部分):
.
├── defaults.json
└── docs
├── 11-01-physics-rotation-and-revolution.html
└── 11-09-physics-mechanical-properties-of-materials.html
我以多種方式嘗試了以下每個命令。
- 我的 PATH 上沒有 Chrome
- 重啟后在我的 PATH 上使用 Chrome(具體來說,該檔案夾
C:\Program Files\Google\Chrome\Application在我的用戶 PATH 上) - 使用
start chrome而不是chrome - 用
\而不是/
PS> chrome ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome Resolve-Path ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> Resolve-Path ./docs/11-09-physics-mechanical-properties-of-materials.html | chrome
PS> Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html | chrome
執行后,Chrome 的地址欄要么有./docs/11-09-physics-mechanical-properties-of-materials.html(不是擴展版,而是字面上獲取.字符)Resolve-Path,,,,Convert-Path要么是空白,我得到了新標簽頁。
以下命令按預期作業:
PS> chrome
PS> chrome google.com
PS> chrome D:\username\Documents\edu\College\attempt-2\Exams\JEE\Notes\self\phy\docs\11-09-physics-mechanical-properties-of-materials.html # this is the full path to the aforementioned phys directory
如何讓它將相對路徑轉換為絕對路徑?來自 Ubuntu 的 bash 幾乎是自動完成的。
uj5u.com熱心網友回復:
您的呼叫Convert-Path應該可以正常作業,并且是處理相對路徑的最佳方式,在這種情況下唯一的問題是您需要使用Grouping 運算子(..),以便在我們將運算式傳遞給之前先評估運算式chrome:
chrome (Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html)
uj5u.com熱心網友回復:
要將相對路徑轉換為絕對路徑,我使用了一個小輔助函式:
function ConvertTo-AbsolutePath ([string]$Path) {
if ($Path.StartsWith("~")) {
$Path = $Path.Substring(1)
$current = $HOME
}
else { $current = $pwd.ProviderPath }
if (-not ([System.IO.Path]::IsPathRooted($Path)) -or $Path -match '^\\[^\\] ') {
return [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($current, $Path))
}
$Path
}
用法:
$absPath = ConvertTo-AbsolutePath './docs/11-09-physics-mechanical-properties-of-materials.html'
然后使用它作為引數來啟動 chrome
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/453180.html
上一篇:System.InvalidOperationException:'sessionnotcreated:此版本的ChromeDriver僅支持使用Selenium和C#的Chrome版本98
