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

问题描述 投票:0回答:1

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

参考:表截图

enter image description here

$url = "https://abcdefg.com" 
$username="[email protected]" 
$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个回答
0
投票

你为什么这样做与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

IE浏览器

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

此时,您需要快速实例化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

© www.soinside.com 2019 - 2024. All rights reserved.