【问题标题】:PowerShell web scraping doesn't work in backgroundPowerShell 网络抓取在后台不起作用
【发布时间】:2021-11-08 18:05:54
【问题描述】:

我写了一个运行良好的脚本:

# Use Internet Explorer
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
# Set Credentials
$username="name.surname@mail.com"
$password="password"
#Navigate to URL
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
# Login 
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
Start-Sleep -seconds 5
# Find file to download
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
$link.click()
Start-Sleep -seconds 3
# Press "Alt + s" on the download dialog  
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")
Start-Sleep -seconds 3
# Quit Internet Explorer
$ie.Quit()

但是,如果我将 $ie.Visible= $true 更改为 $ie.Visible= $false,则脚本将不起作用。

为什么?

因为这两行:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")

在这两行中,我正在处理 Internet Explorer 的下载对话框,如果浏览器在后台运行,则脚本无法点击它。

我如何在后台发送输入,或者如何让 Internet Explorer 始终处于顶部?

【问题讨论】:

    标签: powershell internet-explorer web-scraping scripting background


    【解决方案1】:

    正如您自己的回答所暗示的那样,为了能够将击键发送到应用程序[System.Windows.Forms.SendKeys]::SendWait(),它必须有一个一个窗口( a) 可见 和 (b) 具有(输入)焦点。

    您的答案中显示的技术的更简单和更快的替代方法 - 您使用通过 P/Invoke 声明包装 WinAPI 函数的 C# 代码的临时编译,通过 Add-Type - 如下:

    # Create an Internet Explorer instance and make it visible.
    $ie = New-Object -ComObject 'internetExplorer.Application'; $ie.Visible= $true
    
    # Activate it (give it the focus), via its PID (process ID).
    (New-Object -ComObject WScript.Shell).AppActivate(
      (Get-Process iexplore | Where-Object MainWindowHandle -eq $ie.hWnd).Id
    )
    

    后退一步:

    • GUI 脚本(通过模拟用户输入到 GUI 来自动化任务)本质上是不可靠的;例如,用户可能会点击远离预期具有焦点的窗口。

    • 虽然没有内置解决方案,但听起来 Selenium 提供了强大的程序化浏览器控制。

    【讨论】:

      【解决方案2】:

      我发现的最接近的东西是这个,它丑得要命:

      # Start Internet Explorer on top
      Add-Type -TypeDefinition @"
          using System;
          using System.Runtime.InteropServices;
      
          public class Win32SetWindow {
              [DllImport("user32.dll")]
              [return: MarshalAs(UnmanagedType.Bool)]
              public static extern bool SetForegroundWindow(IntPtr hWnd);
          }
      "@
      
      $ie = new-object -comobject InternetExplorer.Application;
      $ie.visible = $true;
      
          [Win32SetWindow]::SetForegroundWindow($ie.HWND) # <--   Internet Explorer window on top
      
      # Set Credentials
      $username="name.surname@mail.com"
      $password="password"
      #Navigate to URL
      $ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
      While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
      # Login 
      $usernamefield = $ie.document.getElementByID('isiwebuserid')
      $usernamefield.value = "$username"
      $passwordfield = $ie.document.getElementByID('isiwebpasswd')
      $passwordfield.value = "$password"
      $Link = $ie.document.getElementByID('actionLogin')
      $Link.click()
      Start-Sleep -seconds 5
      # Find file to download
      $link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
      $link.click()
      Start-Sleep -seconds 3
      # Press "Alt + s" on the download dialog  
      Add-Type -AssemblyName System.Windows.Forms
      [System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}")     # or use SendWait("%s")
      Start-Sleep -seconds 3
      # Quit Internet Explorer
      $ie.Quit()
      

      【讨论】:

      • 很高兴看到一个使用 PowerShell 中的 P/Invoke 的示例(尽管在这种情况下证明不需要它)。请注意,Add-Type 具有用于创建 P/Invoke 包装器的语法糖,即当您使用 -MemberDefinition 而不是 -TypeDefinition 时;有关背景信息,请参阅this answer 的底部。
      • 感谢您的帮助@mklement0。但是我对我的代码并不完全满意。如果用户强行将其放在背景上,或者用户在此过程中单击另一个窗口,则输入焦点将丢失。您还有其他方法可以登录网页并下载吗?
      • 是的,GUI 脚本总是很脆弱。我没有个人经验,但也许Selenium 可以提供帮助;第三方 Selenium PowerShell Module 是它的 PowerShell 包装器,但我不知道它是否仍然有效(该项目正在寻找维护者)。
      猜你喜欢
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 2020-11-10
      • 2020-02-09
      • 1970-01-01
      • 2020-06-24
      • 2017-11-09
      相关资源
      最近更新 更多