You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell在Microsoft Edge中以标签组形式打开多个文件

使用Selenium WebDriver在Edge/Chrome/Firefox中创建标签组并打开文件

由于Selenium原生没有提供标签组的直接操作API,我们可以通过模拟键盘快捷键或浏览器UI交互的方式实现标签组的创建和文件批量添加,以下是针对各浏览器的具体方案:


Microsoft Edge(优先方案)

Edge的标签组可通过Ctrl+Shift+G快捷键快速创建和管理,结合Selenium的键盘模拟功能实现需求:

PowerShell代码示例

Import-Module Selenium

# 替换为你的EdgeDriver路径(需与浏览器版本完全匹配)
$edgeDriverPath = "C:\Tools\msedgedriver.exe"

# 初始化Edge配置
$edgeOptions = New-Object OpenQA.Selenium.Edge.EdgeOptions
$edgeOptions.AddArgument("--start-maximized")
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver($edgeDriverPath, $edgeOptions)

# 定义标签组:键为组名,值为要打开的本地文件路径列表
$tabGroups = @{
    "项目文档集" = @("C:\Docs\project-plan.pdf", "C:\Docs\requirements.txt", "C:\Docs\design.html")
    "参考素材库" = @("C:\Images\mockup.jpg", "C:\Images\logo.png")
}

$keys = [OpenQA.Selenium.Keys]

foreach ($group in $tabGroups.GetEnumerator()) {
    $groupName = $group.Key
    $files = $group.Value

    # 打开组内第一个文件作为组的起始标签
    $firstFile = "file:///$($files[0].Replace('\', '/'))"
    $driver.Navigate().GoToUrl($firstFile)

    # 触发创建标签组快捷键
    $driver.FindElement([OpenQA.Selenium.By]::TagName("body")).SendKeys($keys::Control + $keys::Shift + "g")
    Start-Sleep -Milliseconds 500 # 等待组名输入框加载

    # 输入组名并确认
    $driver.FindElement([OpenQA.Selenium.By]::TagName("body")).SendKeys($groupName + $keys::Enter)
    Start-Sleep -Milliseconds 300

    # 批量添加剩余文件到当前组
    $currentWindow = $driver.CurrentWindowHandle
    foreach ($file in $files[1..($files.Count-1)]) {
        $fileUrl = "file:///$($file.Replace('\', '/'))"
        # 打开新标签页
        $driver.ExecuteScript("window.open('$fileUrl');")
        # 切换到新标签
        $newWindow = $driver.WindowHandles | Where-Object { $_ -ne $currentWindow }
        $driver.SwitchTo().Window($newWindow[-1])
        $currentWindow = $newWindow[-1]

        # 打开标签组选择菜单,添加到目标组
        $driver.FindElement([OpenQA.Selenium.By]::TagName("body")).SendKeys($keys::Control + $keys::Shift + "g")
        Start-Sleep -Milliseconds 500
        $driver.FindElement([OpenQA.Selenium.By]::TagName("body")).SendKeys($groupName + $keys::Enter)
        Start-Sleep -Milliseconds 300
    }
}

# 等待用户操作后关闭浏览器(可根据需求删除)
Read-Host "按任意键关闭浏览器"
$driver.Quit()

注意事项

  • 务必保证EdgeDriver版本与Edge浏览器完全一致,版本不匹配会导致启动失败
  • 若Ctrl+Shift+G快捷键无效,可在Edge设置中查看当前标签组快捷键(设置→系统和性能→快捷键)

Google Chrome方案

Chrome的标签组操作逻辑与Edge完全一致,仅需替换驱动为ChromeDriver:

PowerShell代码示例

Import-Module Selenium

# 替换为你的ChromeDriver路径
$chromeDriverPath = "C:\Tools\chromedriver.exe"

$chromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$chromeOptions.AddArgument("--start-maximized")
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($chromeDriverPath, $chromeOptions)

# 标签组配置和后续操作与Edge示例完全相同,直接复用即可
# ...(复制上述Edge示例中的tabGroups定义和循环逻辑)

Mozilla Firefox方案

Firefox的标签组称为「标签容器」,快捷键为Ctrl+Shift+E,需调整UI交互逻辑:

PowerShell代码示例

Import-Module Selenium

# 替换为你的GeckoDriver路径
$firefoxDriverPath = "C:\Tools\geckodriver.exe"

$firefoxOptions = New-Object OpenQA.Selenium.Firefox.FirefoxOptions
$firefoxOptions.AddArgument("--start-maximized")
$driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver($firefoxDriverPath, $firefoxOptions)

$tabGroups = @{
    "工作文件包" = @("C:\Files\task1.docx", "C:\Files\data.csv")
    "学习资料库" = @("C:\Docs\tutorial.pdf", "C:\Docs\notes.md")
}

$keys = [OpenQA.Selenium.Keys]

foreach ($group in $tabGroups.GetEnumerator()) {
    $groupName = $group.Key
    $files = $group.Value

    # 打开组内第一个文件
    $firstFile = "file:///$($files[0].Replace('\', '/'))"
    $driver.Navigate().GoToUrl($firstFile)

    # 触发创建标签容器快捷键
    $driver.FindElement([OpenQA.Selenium.By]::TagName("body")).SendKeys($keys::Control + $keys::Shift + "e")
    Start-Sleep -Milliseconds 500

    # 输入容器名并确认
    $driver.FindElement([OpenQA.Selenium.By]::CssSelector(".tab-container-panel input")).SendKeys($groupName + $keys::Enter)
    Start-Sleep -Milliseconds 300

    # 批量添加剩余文件到容器
    $currentWindow = $driver.CurrentWindowHandle
    foreach ($file in $files[1..($files.Count-1)]) {
        $fileUrl = "file:///$($file.Replace('\', '/'))"
        $driver.ExecuteScript("window.open('$fileUrl');")
        $newWindow = $driver.WindowHandles | Where-Object { $_ -ne $currentWindow }
        $driver.SwitchTo().Window($newWindow[-1])
        $currentWindow = $newWindow[-1]

        # 打开容器选择菜单并添加到目标组
        $driver.FindElement([OpenQA.Selenium.By]::TagName("body")).SendKeys($keys::Control + $keys::Shift + "e")
        Start-Sleep -Milliseconds 500
        $driver.FindElement([OpenQA.Selenium.By]::CssSelector(".tab-container-panel input")).SendKeys($groupName)
        Start-Sleep -Milliseconds 200
        $driver.FindElement([OpenQA.Selenium.By]::TagName("body")).SendKeys($keys::Enter)
        Start-Sleep -Milliseconds 300
    }
}

Read-Host "按任意键关闭浏览器"
$driver.Quit()

注意事项

  • Firefox的标签容器UI选择器可能随版本更新变化,若元素定位失效,可通过浏览器开发者工具获取最新选择器

内容的提问来源于stack exchange,提问作者TimSch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 05:47:51