【问题标题】:Download file from website using SendKeys in Powershell使用 Powershell 中的 SendKeys 从网站下载文件
【发布时间】:2019-03-07 17:34:24
【问题描述】:

我正在尝试通过单击文件图标从特定网站下载文件。网站登录有效,但我希望使用击键“TAB”导航到 Excel 文件,最后按“Enter”进行下载。运行代码,但导致 Powershell 文本为“FALSE”。任何建议表示赞赏!谢谢。

参考:表格截图

$url = "https://abcdefg.com" 
$username="test@gmail.com" 
$password="TestPW" 
$ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy -eq $true) 
{ 
    Start-Sleep -Milliseconds 1000; 
} 
$ie.Document.getElementById("txtEmail").value = $username 
$ie.Document.getElementByID("txtPassword").value=$password 
$ie.Document.getElementById("Login").Click();

Start-Sleep -Milliseconds 10000

$obj = new-object -com WScript.Shell
$obj.AppActivate('Internet Explorer')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{Enter}')

【问题讨论】:

    标签: powershell sendkeys


    【解决方案1】:

    您为什么要这样做而不是 using web scraping 来查找您要点击的链接,并直接使用链接 URL?

    您的帖子确实是此问答的副本。

    Use PowerShell to automate website login and file download

    SendKeys 可以工作,但它们非常古怪,并且在不同的系统上可能无法按您预期的那样运行。有更好的工具专门用于执行此操作,AutoITSeleniumWASP

    --- 那个 WASP 工具仍然有效,但很长时间没有更新。

    Using PowerShell 2.0 With Selenium to Automate Internet Explorer, Firefox, and Chrome

    Internet Explorer

    接下来您要从该站点获取 Internet Explorer 驱动程序。我 推荐 2.41 版,因为“截至 2014 年 4 月 15 日,IE 6 不再是 支持的”。这必须位于您当前的 PATH 中,因此在您的脚本中 您可能需要修改 PATH 以确保可执行文件 (IEDriverServer.exe) 可以在那里找到。如果您想知道是否 要获取 32 位或 64 位版本,请从 32 位开始,即使 你有一个 64 位的 Windows。

    此时您需要快速实例化 Internet Explorer 和 导航某处。伟大的。让我们去做吧。

    # Load the Selenium .Net library
    Add-Type -Path "C:\selenium\WebDriver.dll" # put your DLL on a local hard drive!
    
    # Set the PATH to ensure IEDriverServer.exe can found
    $env:PATH += ";N:\selenium"
    
    # Instantiate Internet Explorer
    $ie_object = New-Object "OpenQA.Selenium.IE.InternetExplorerDriver"
    
    
    # Great! Now we have an Internet Explorer window appear. We can navigate to a new URL:
    $ie_object.Navigate().GoToURL( "http://www.bbc.co.uk/languages" )
    
    # This worked! The call won’t return until the page download is complete.
    # Next let’s click on a link from the link text:
    $link = $ie_object.FindElementByLinkText( "Spanish" )
    $link.Click()
    
    # display current URL
    $ie_object.Url
    

    Selenium Tutorial: All You Need To Know About Selenium WebDriver

    OP 更新

    至于……

    但是该文件没有重定向的 URL

    然后您需要更深入地查看该站点,以找到您可以强制点击的文件的锚点。

    例子:

    # Scrape a web page with PowerShell
    
    $w = Invoke-WebRequest -Uri 'https://www.reddit.com/r/PowerShell'
    $w | Get-Member
    
    $w.AllElements
    $w.AllElements.Count
    $w.Links.Count
    $w.Links
    
    $w.Forms
    $w.Forms.Fields
    
    $w.Forms[0]
    $w.Forms[0].Fields
    
    $w.RawContent
    
    $w.ParsedHtml
    

    一旦找到标签名称或类似名称,您需要对其进行解析以从中获取内容。

    $w.AllElements | Where-Object -Property 'TagName' -EQ 'P' | Select-Object -Property 'InnerText'
    

    对于表格,您必须挖掘更多。

    Extracting Tables from PowerShell’s Invoke-WebRequest

    【讨论】:

    • 感谢您的回复。但是,该文件没有重定向 URL,例如 www.abcdef/file/test.pdf。相反,它具有以下标签:
    猜你喜欢
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2023-03-14
    相关资源
    最近更新 更多