我正在尝试通过单击文件图标从特定网站下载文件。网站登录有效,但我希望使用击键“ 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}')
答案 0 :(得分:0)
与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的更新
至于...
但是文件没有重定向的网址
然后,您需要更深入地浏览该站点,以找到可以强制单击的文件的锚点。
示例:
# 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'
对于表格,您必须进行更多挖掘。