Chinaunix首页 | 论坛 | 博客
  • 博客访问: 25138
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-26 11:35
文章分类

全部博文(5)

文章存档

2015年(5)

我的朋友

分类: 系统运维

2015-07-02 09:11:43

因实际需要把某测试网站的测试结果抓取下来,但是又不想人工在做这个工作,就想通过自动化的方式来完成该工作.
实际分析发现,结果文件xml在远端服务器上,最终该文件的路径是自动生产的.

window.open(''+encodeURIComponent(result_xml)+'&fn='+encodeURIComponent(fname))"

无法直接下载,于是借鉴这段代码

Function GetCurrentIEURL
{
    $IEObjs = @()
    $ShellWindows = (New-Object -ComObject Shell.Application).Windows()

    Foreach($IE in $ShellWindows)
    {
        $FullName = $IE.FullName
        If($FullName -ne $NULL)
        {
            $FileName = Split-Path -Path $FullName -Leaf

            If($FileName.ToLower() -eq "iexplore.exe")
            {
                $Title = $IE.LocationName
                $URL = $IE.LocationURL
                $IEObj = New-Object -TypeName PSObject -Property @{Title = $Title; URL = $URL}
                $IEObjs += $IEObj
            }
        }
    }

    $IEObjs
}

来抓取那一瞬间产生的文件路径.下面就通过判断抓取的路径进行下载.
Function DownloadTestResults()
{
$ie = New-Object -COM InternetExplorer.Application
$ie.Navigate("

$latenote = $ie.Document.getElementById("latnote")
while( $ie.busy){Start-Sleep 1}
Write-Host $latenote
$downloadresults = $ie.Document.getElementById("drbt")
while( $ie.busy){Start-Sleep 10}
Start-Sleep -s 100
$downloadresults.click()
$CurrentIEURL = GetCurrentIEURL
    If($CurrentIEURL -ne "
        Write-Host "Successfully exported csv file on '$ExportCsvFile'"
       
        $src = $null
        Import-Csv C:\Users\Public\1.csv|Where-Object {
        if($_.URL -eq $_.Title){
            $src = $_.URL
         Write-Host $_.URL
     }
       
    }
    #$src = ''
    $date = Get-Date
    $name = $date.ToString('yyyy-MM-dd-hh-mm')
    #Destination Folder
    $destination = New-Item -Path c:\users\public\PACResults\$name.csv -ItemType file -Force
  

    Invoke-WebRequest -uri $src+".csv" -OutFile $destination
    Unblock-File $destination
    }
    Else
    {
        Write-Warning "Can not find any URL of Tab."
    }

 
}

阅读(1590) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~