微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Powershell 和 Microsoft Edge 自动化

如何解决Powershell 和 Microsoft Edge 自动化

我正在尝试创建一个脚本,该脚本可自动将 Microsoft Edge 打开到开发人员模式,并在现有选项卡中执行 bing 搜索

我已经设法启动 Edge 并将选项卡置于开发人员模式,但是如果 Itry 并添加命令以完成 bing 搜索,它会打开一个辅助选项卡,该选项卡不在开发人员模式下。

我的目标是—— 以最大化窗口的形式打开 MSEDGE - 在当前选项卡 (F12) 按键中启用开发者模式选项 - 在当前选项卡中完成 bing 搜索(为了论证“https://www.bing.com/search?q=1”)

到目前为止我的代码包括-

& "C:\Program Files  (x86)\Microsoft\Edge\Application\msedge.exe" -ArgumentList --profile-directory="Profile 3"
Sleep 5 
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Google - Microsoft Edge') 
$wshell.SendKeys('{F12}')

非常感谢任何帮助。

谢谢 埃尔达火车

解决方法

继续我的评论。那不是MSEdge的位置,至少在Win10上不是。

这样做...

& "C:\Program Files  (x86)\Microsoft\Edge\Application\msedge.exe"

...在默认的 Win10 安装中只会出错。

& : 术语 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

Edge 是一款现代应用,它们位于一个特殊的文件夹中。

Get-AppxPackage -Name '*edge*' | 
Select-Object -Property Name,InstallLocation
<#
Name                                  InstallLocation                                                          
----                                  ---------------                                                          
Microsoft.MicrosoftEdge               C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe              
Microsoft.MicrosoftEdgeDevToolsClient C:\Windows\SystemApps\Microsoft.MicrosoftEdgeDevToolsClient_8wekyb3d8bbwe
#>

此外,您使用的应用名称无效:

Get-ChildItem -Path "$env:windir\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" -Filter '*edge*'

# Results
<#
    Directory: C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe


Mode                 LastWriteTime         Length Name 
----                 -------------         ------ ---- 
-a----        10/15/2020  12:53 AM       16305480 MicrosoftEdge.exe
#>

尽管如此,按照设计,Microsoft Edge 并不像 IE 那样支持 COM 自动化。你需要使用这个需要下载的WebDriver。

https://www.microsoft.com/en-us/download/details.aspx?id=48212

在 Microsoft Edge WebDrive 页面上查看详细信息。

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver

所有这一切,您都可以使用链接从 PowerShell 启动 MSEdge。

示例 - 默认桌面链接

& (Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*edge*').FullName

Start-Process -FilePath (Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*edge*').FullName

一旦 MS Edge 打开并可见,您就可以使用 SendKeys 来处理它。

Add-Type -AssemblyName System.Windows.Forms
& (Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*edge*').FullName
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait('{F12}')

最后,无需为您的用例单独启动 MSEdge。只需启动 URL,它就会启动您的默认浏览器。

Add-Type -AssemblyName System.Windows.Forms
Start-Process -FilePath 'https://www.google.com'
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait('{F12}')
,

从描述中,我了解到您想使用 Powershell 启动 Edge Chromium 浏览器,然后启动开发者工具并访问特定 URL。

您可以尝试使用 URL 启动 Edge 浏览器,然后尝试打开开发者工具。

请参考下面的例子。

[system.Diagnostics.Process]::Start("msedge","https://www.bing.com/search?q=1")

Sleep 3 
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Bing') 
$wshell.SendKeys('{F12}')

输出:

enter image description here

注意:请确保首先以最大化模式手动启动 Edge 浏览器并关闭它,然后尝试运行上述脚本将以最大化模式启动 Edge 浏览器。

如果您想执行一些高级自动化,那么建议使用 MS Webdriver

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。