Reputation: 761
I have an array, $Conventional, which I need to loop through and click each time so that I can save the PDFs which are made available after the click().
$Conventional = @()
$Conventional = $ie.Document.getElementsByTagName("td") | ? {($_.getAttributeNode('class').Value -match 'NodeDocument') -and ($_.innerText -notmatch 'Library Home')}
This fills $Conventional with four td
elements which I need to loop through and click() each time. The following is my ForEach loop which works fine on the first iteration, however it then fails and returns System.ComObject
each time after that.
ForEach ($i in $Conventional){
$text = $i.innerText
$i.click()
while ($ie.Busy -eq $true){Start-Sleep -Seconds 2}
$PDF = $ie.Document.getElementById("OurLibrary_LibTocUC_LandingPanel_libdocview1_DocViewDocMD1_hlViewDocument")
$currentURL = $PDF.href
$fileName = $baseFileName + "_" + $cleanText
Invoke-WebRequest -Uri $currentURL -OutFile $NewPath\$fileName.pdf -WebSession $freedom
}
Here is a screenshot of the array I am capturing. In order to retrieve the PDFs, each one needs to be clicked.
Any help would really be appreciated. Thanks everyone
Upvotes: 2
Views: 1412
Reputation: 54981
Since it works fine unless you press click, then maybe the click-event changes the Document enough to break the element-referenses in your $Conventional
-array. Try this approach:
$linksToProcess = New-Object System.Collections.ArrayList
$ie.Document.getElementsByTagName("td") |
Where-Object {($_.getAttributeNode('class').Value -match 'NodeDocument') -and ($_.innerText -notmatch 'Library Home')} |
Foreach-Object { $linksToProcess.Add($_.innerText) }
while ($linksToProcess.Count -gt 0){
$i = $ie.Document.getElementsByTagName("td") | ? {($_.getAttributeNode('class').Value -match 'NodeDocument') -and ($_.innerText -eq $linksToProcess[0])}
$text = $i.innerText
$i.click()
while ($ie.Busy -eq $true){Start-Sleep -Seconds 2}
$PDF = $ie.Document.getElementById("OurLibrary_LibTocUC_LandingPanel_libdocview1_DocViewDocMD1_hlViewDocument")
$currentURL = $PDF.href
$fileName = $baseFileName + "_" + $cleanText
Invoke-WebRequest -Uri $currentURL -OutFile $NewPath\$fileName.pdf -WebSession $freedom
$linksToProcess.RemoveAt(0)
}
Upvotes: 1