如何使用FlaUI设置字段的值?

huangapple go评论43阅读模式
英文:

How to set value in field using FlaUI?

问题

使用FlaUI在单击“另存为”按钮后设置特定字段中的值的方法是什么?

我正在尝试使用FlaUI在单击“另存为”后自动填充画图中的一个字段。涉及的字段名为“名称:”。以下是我迄今为止的代码:

Add-Type -Path C:\Users\sergi\assemblies\bin\Release\net48\publish\FlaUI.UIA3.dll

$windowTitle = 'Untitled - Paint'
$control = '名称:'
$automation = [FlaUI.UIA3.UIA3Automation]::new()
$process = get-process | Where-Object {$_.MainWindowTitle -match $windowTitle}
$app = [FlaUI.Core.Application]::Attach( $process )

foreach( $wnd in $app.GetAllTopLevelWindows( $automation ) ) {
    $myinput = $wnd.FindAllDescendants() | Where-Object { $_.Name -eq $control }
    $myinput[0].SetValue('值测试')
}

运行代码后出现以下错误:

方法调用失败,因为[FlaUI.Core.AutomationElements.AutomationElement]不包含名为'SetValue'的方法。
在第11行字符:5
+ $saveButton[0].SetValue('Value Test')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo: InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId: MethodNotFound

然而,我很难找到一种使用FlaUI访问此特定字段并设置其值的方法。您有关于如何实现这一目标的建议吗?

英文:

How do I use FlaUI to set the value in a specific field after clicking the "Save As" button in Paint?

I'm trying to use FlaUI to automate filling a field in Paint after clicking "Save As". The field in question is named "Name:". Here is the code I have so far:

Add-Type -Path C:\Users\sergi\assemblies\bin\Release\net48\publish\FlaUI.UIA3.dll

$windowTitle = 'Untitled - Paint'
$control = 'Name:'
$automation = [FlaUI.UIA3.UIA3Automation]::new()
$process = get-process | Where-Object {$_.MainWindowTitle -match $windowTitle}
$app = [FlaUI.Core.Application]::Attach( $process )

foreach( $wnd in $app.GetAllTopLevelWindows( $automation ) ) {
    $myinput = $wnd.FindAllDescendants() | Where-Object { $_.Name -eq $control }
    $myinput[0].SetValue('Value Test')
}

After running the code it gives this error:

Method invocation failed because [FlaUI.Core.AutomationElements.AutomationElement] does not contain a method named 'SetValue'.
On line: 11 character: 5
+ $saveButton[0].SetValue('Value Test')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

However, I'm having a hard time finding a way to access this particular field and set its value using FlaUI. Any suggestions on how I can do this?

答案1

得分: 2

以下是代码部分的中文翻译:

# 创建UI自动化对象
$automation = [FlaUI.UIA3.UIA3Automation]::new()

# 遍历所有 mspaint 进程
foreach ($process in Get-Process mspaint) {
    $app = [FlaUI.Core.Application]::Attach($process)

    # 遍历应用程序的顶层窗口
    foreach ($wnd in $app.GetAllTopLevelWindows($automation)) {

        # 查找“保存”按钮
        $saveButton = $wnd.FindAllDescendants() | 
                      Where-Object { $_.ControlType -eq 'Button' -and $_.Name -eq 'Save' }

        Write-Host "找到 '保存' 按钮"

        # 调用按钮的模式以触发点击
        $saveButton.Patterns.Invoke.Pattern.Invoke()

        Write-Host '等待 "保存" 对话框...'

        $saveDialog = $fileNameField = $null

        # 循环直到出现“保存”对话框并可以输入
        while ($true) {
            $saveDialog = $wnd.FindAllChildren() | Where-Object Name -eq '另存为'
            if ($saveDialog -and $saveDialog.IsEnabled) {
                Write-Host '找到 "保存" 对话框'

                # 查找文件名编辑控件
                $fileNameField = $saveDialog.FindAllDescendants() | 
                                 Where-Object { $_.ControlType -eq 'Edit' -and $_.AutomationId -eq '1001' -and $_.IsEnabled }
                if ($fileNameField) {
                    Write-Host '找到文件名文本框'
                    break
                }
            }
            Start-Sleep 1
        }

        $savePath = 'C:\test\paint.png'

        # 删除现有文件以便检查是否已保存文件
        $savePath | Where-Object { Test-Path $_ -PathType Leaf } | Remove-Item -Force

        # 将路径输入到文件名编辑控件
        $fileNameField.Patterns.Value.Pattern.SetValue($savePath)

        # 查找并点击确定按钮
        $okButton = $saveDialog.FindAllChildren() | 
                    Where-Object { $_.ControlType -eq 'Button' -and $_.AutomationId -eq '1' }
        $okButton.Patterns.Invoke.Pattern.Invoke()

        # 等待 mspaint 保存文件
        while (-not (Test-Path $savePath)) {
            Start-Sleep 1
        }
        Write-Host "文件已保存!"
    }
}

请注意,以上翻译只包括代码部分,不包括注释部分。如果您需要更多帮助,请随时提出问题。

英文:

After pressing the "save" button, you have to search for the "save" dialog window. There might be a delay before the dialog and its child elements have been created, so for reliability a loop should be used.

I recommend to install Accessibility Insights for Windows to get an idea what you need to search for. It shows you the hierarchy of automation objects and their properties.

Here is working code to automate the "save" dialog.

$automation = [FlaUI.UIA3.UIA3Automation]::new()

foreach( $process in Get-Process mspaint ) {
    $app = [FlaUI.Core.Application]::Attach( $process )

    foreach( $wnd in $app.GetAllTopLevelWindows( $automation ) ) {

        $saveButton = $wnd.FindAllDescendants() | 
                      Where-Object { $_.ControlType -eq 'Button' -and $_.Name -eq 'Save' }         

        Write-Host "Found 'save' button"

        $saveButton.Patterns.Invoke.Pattern.Invoke()

        Write-Host 'Waiting for "save" dialog...'

        $saveDialog = $fileNameField = $null

        # Loop until the "save" dialog has been shown and accepts input
        while( $true ) {
            $saveDialog = $wnd.FindAllChildren() | Where-Object Name -eq 'Save as'
            if( $saveDialog -and $saveDialog.IsEnabled ) {
                Write-Host 'Found "save" dialog'

                # Search for the file name edit control
                $fileNameField = $saveDialog.FindAllDescendants() | 
                                 Where-Object { $_.ControlType -eq 'Edit' -and $_.AutomationId -eq '1001' -and $_.IsEnabled }
                if( $fileNameField ) {
                    Write-Host 'Found file name text field'
                    break
                }
            }
            Start-Sleep 1
        }

        $savePath = 'C:\test\paint.png'

        # Delete any existing file so we can check if mspaint has saved the file
        $savePath | Where-Object { Test-Path $_ -PathType Leaf } | Remove-Item -Force

        # Enter the path into the file name edit control
        $fileNameField.Patterns.Value.Pattern.SetValue( $savePath )

        # Find and press the OK button
        $okButton = $saveDialog.FindAllChildren() | 
                    Where-Object { $_.ControlType -eq 'Button' -and $_.AutomationId -eq '1' }
        $okButton.Patterns.Invoke.Pattern.Invoke()

        # Wait until file has been saved by mspaint
        while( -not ( Test-Path $savePath ) ) {
            Start-Sleep 1
        }
        Write-Host "File has been saved!"
    }
}

huangapple
  • 本文由 发表于 2023年2月19日 23:55:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定