在 Powershell 中使用 SendKeys 从网站下载文件

Download file from website using SendKeys in Powershell

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

参考:Table截图

$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}')

你为什么要这样做而不是 using web scraping 来找到你想要击中的 link,然后直接使用 link URL?

您的 post 确实是此问答的重复。

Use PowerShell to automate website login and file download

SendKeys 可以工作,但它们非常笨拙,并且在不同的系统上可能无法像您预期的那样工作。有更好的工具专门用于执行此操作,AutoIT, Selenium, WASP

--- 那个 WASP 工具仍然可以使用,但是已经很久没有更新了。

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

Internet Explorer

Next you want to obtain the Internet Explorer driver from this site. I recommend version 2.41 because “as of 15 April 2014, IE 6 is no longer supported”. This must reside in your current PATH so in your script you may want to modify your PATH to ensure the executable (IEDriverServer.exe) can be found there. If you’re wondering whether to get the 32-bit or the 64-bit version, start with the 32-bit even if you’ve got a 64-bit Windows.

At this point you’ll want to quickly instantiate Internet Explorer and navigate somewhere. Great. Let’s do it.

# 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 更新

至于...

However the file does not have a redirected 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