2

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. screenshot of $Conventional array

Any help would really be appreciated. Thanks everyone

5
  • 1
    foreach(){} sometimes have problems with the collections returned by COM applications because they don't properly implement IEnumerable. Try with $Conventional |ForEach-Object { $_.InnerText } instead Commented Apr 19, 2016 at 15:21
  • Thanks for the response, trying this now ! Commented Apr 19, 2016 at 15:57
  • Hmm, same issue doing it the way you suggested. After the first iteration, the Array is empty and nothing works. When I remove the $_click() it works and prints the innerText but I need it to work with the $_click() also. Grrrr... Commented Apr 19, 2016 at 16:09
  • 1
    Instead of using @() to instantiate a powershell array variable, use a .Net array with = New-Object System.Collections.ArrayList then attempt to add to your array. I wouldn't trust powershell to automatically box the output from $ie..getElementsById() so I'd work in powershell to make sure your elements are being returned first, then if they are being casted to your array properly second, and then work with each element in a foreach loop after confirming the elements are being formatted correctly. Commented Apr 19, 2016 at 16:50
  • Good thinking, thanks. I will attempt to implement this. Commented Apr 19, 2016 at 18:40

1 Answer 1

1

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)
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. I am trying this now.
Out of curiosity, is there any reason you chose to use a 'while' loop and pop the values off the array, rather than using a For loop with a counter?
That doesn't matter. I would actually use a foreach-loop since its only a list of strings. The while loop was leftover from a previous idea :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.