如何使用 Powershell 中的 Edge Selenium webdriver 检查 window 是否被最小化

How to check if window is minimized with Edge Selenium webdriver in Powershell

有人会认为这应该是一件容易的事,但我找不到一种方法来检测 Chromium Edge Selenium webdriver Window 是否被最小化或不在 Powershell 中。

具体来说,一个Window的大小和位置似乎是一样的,不管它是处于最大化还是最小化状态。举个例子(从正常的非最小化window状态开始):

> $driver.manage().Window.size

IsEmpty Width Height
------- ----- ------
  False  1050    708

> $driver.manage().Window.position

IsEmpty  X  Y
-------  -  -
  False 13 18

> $driver.manage().Window.minimize()
> $driver.manage().Window.size

IsEmpty Width Height
------- ----- ------
  False  1050    708

> $driver.manage().Window.position

IsEmpty  X  Y
-------  -  -
  False 13 18

如您所见,即使 Window 已最小化,Window 大小和位置仍保持不变。

我在任何地方都找不到 isMinimized() 方法或类似的东西。

Chromium Edge 网络驱动程序版本为 93.0.961.38。

有什么想法吗?

您可以尝试使用 Selenium 驱动程序执行 JavaScript 来获取视口的大小(没有 powershell,但在我们的场景中有效)。

const width  = window.innerWidth || document.documentElement.clientWidth || 
document.body.clientWidth;
const height = window.innerHeight|| document.documentElement.clientHeight|| 
document.body.clientHeight;

参考:
Get the size of the screen, current web page and browser window

用powershell访问进程的WindowVisualState:

Add-Type -AssemblyName UIAutomationClient
$prList = Get-Process -Name "notepad"
$prList | % {
    $ae = [System.Windows.Automation.AutomationElement]::FromHandle($_.MainWindowHandle)
    $wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
    echo "Window visual state: $($wp.Current.WindowVisualState)"
}

结果:

Window visual state: Minimized

参考:
Get window state of another process

我的解决方案是强制启动一个 selenium edgedriver 实例最大化。因此,对 edgedriver 实例化前后的进程列表进行比较和过滤,以得到 'msedge' 进程,然后先最小化,然后最大化。

$workingPath = 'C:\selenium'

if (($env:Path -split ';') -notcontains $workingPath) {
    $env:Path += ";$workingPath"
}

[System.Reflection.Assembly]::LoadFrom("$($workingPath)\WebDriver.dll")

# get all listening processes before new edgedriver is started
$processesBefore = $(netstat -aon | findstr LISTENING)

# launch edgedriver
$EdgeDriver = New-Object OpenQA.Selenium.Edge.EdgeDriver

# get all listening processes after edgedriver started
$processesAfter = $(netstat -aon | findstr LISTENING)

# get the differencing processes (find the new processes)
$processesDiff = Compare-Object -ReferenceObject $processesBefore -DifferenceObject $processesAfter

# get the process ids of new processes
$processIdArray = foreach($process in $processesDiff) {
    $process.InputObject.Split(' ')[-1]
}

# get the msedge process
$p = Get-Process -id $processIdArray | where name -eq msedge

# source of howto maximize a window: https://community.spiceworks.com/topic/664020-maximize-an-open-window-with-powershell-win7
Add-Type '[DllImport("user32.dll")] public static extern bool ShowWindowAsync (IntPtr hwnd, int nCmdShow);' -Name Win32ShowWindowAsync -Namespace Win32Functions

# first minimize the window so it can be maximized
$null = [Win32Functions.Win32ShowWindowAsync]::ShowWindowAsync($p.MainWindowHandle, 6)

# lastly maximize the window
$null = [Win32Functions.Win32ShowWindowAsync]::ShowWindowAsync($p.MainWindowHandle, 3)

# navigate to web page and do stuff
$EdgeDriver.Navigate().GoToUrl('https://example.org')

最好的办法是强制启动selenium edgedriver。

它会强制最小化。