【问题标题】:How to check if window is minimized with Edge Selenium webdriver in Powershell如何使用 Powershell 中的 Edge Selenium webdriver 检查窗口是否已最小化
【发布时间】:2023-12-30 02:11:01
【问题描述】:

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

具体来说,一个窗口的大小和位置似乎是相同的,无论它是处于最大化还是最小化状态。比如下面的例子(从一个正常的非最小化窗口状态开始):

> $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

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

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

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

有什么想法吗?

【问题讨论】:

  • 您可以将--start-maximized 添加为驱动程序的参数,以便在启动时获得定义的状态,然后自行跟踪状态。
  • @stackprotector 好主意,但我很想知道窗口是否被最小化,无论之前的状态如何,即是否最大化。
  • @GuentherSchmitz 非常有趣,但一个问题是在任何给定时间都可能打开多个 Edge 窗口。我只对 selenium webdriver 实例化的 Edge 窗口是否最小化感兴趣(不是用户单独实例化的任何 Edge 窗口)。

标签: powershell selenium selenium-edgedriver microsoft-edge-chromium


【解决方案1】:

您可以尝试使用 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

【讨论】:

  • 感谢您的回答,尽管 Powershell 的回答会更理想。
  • 我在 Powershell 中添加了代码 sn-p,希望对您有所帮助。
【解决方案2】:

我的解决方案是强制启动 selenium 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')

【讨论】:

    【解决方案3】:

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

    它会强制最小化。

    【讨论】:

      最近更新 更多