您为什么要这样做而不是 using web scraping 来查找您要点击的链接,并直接使用链接 URL?
您的帖子确实是此问答的副本。
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
接下来您要从该站点获取 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