Download an image from website

2

I ran this powershell script to download an image from a website (to download it, certain steps had to be made, that's why I used IE navigate). I put a random string with a space between 4 and 4 characters.

But I get an error and it doesn't even start to fill the blank with the string:

Exception from HRESULT: 0x800A01B6
At E:\getbd.ps1:13 char:1
+ $ie.Document.getElementsByTagName("text") | where { $.name -eq "words ...

Here is the code:

$url = "https://fakecaptcha.com"
$set = "abcdefghijklmnopqrstuvwxyz0123456789".ToCharArray()
for($i=1; $i -le 4; $i++){
$result += $set | Get-Random}
$result += ' '
for($i=1; $i -le 4; $i++){
$result += $set | Get-Random}
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1}
$ie.Document.getElementsByTagName("text") | where { $.name -eq "words" }.value = $result
$generateBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'submit' -and $_.Value -eq 'Create it now!'} 
$generateBtn.click() 
while( $ie.busy){Start-Sleep 1}
$readyBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'Your captcha is done! Please click here to view it!!'} 
$readyBtn.click() 
while( $wc.busy){Start-Sleep 1}
$downloadBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'DOWNLOAD'} 
$downloadBtn.click()
while( $ie.busy){Start-Sleep 1}
$source = $ie.document.getElementsByTagName('img') | Select-Object -ExpandProperty src  
$file = '$E:\bdlic\'+$result+'.jpg'
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source,$file)
while( $wc.busy){Start-Sleep 1}
$ie.quit()
powershell
asked on Stack Overflow Mar 5, 2016 by Alex Mihaila • edited Mar 5, 2016 by Ansgar Wiechers

1 Answer

1

You have 2 syntax errors in that line:

$ie.Document.getElementsByTagName("text") | where { $.name -eq "words" }.value = $result
#                                                   ^                   ^^^^^^
  1. $.Name: The "current object" variable is $_, not just $.
  2. where {...}.value: You cannot use dot-notation on the scriptblock of a Where-Object statement. You need to put the entire statement in a (sub)expression for that.

Change the line to this:

($ie.Document.getElementsByTagName("text") | where { $_.name -eq "words" }).value = $result
answered on Stack Overflow Mar 5, 2016 by Ansgar Wiechers • edited Mar 6, 2016 by Ansgar Wiechers

User contributions licensed under CC BY-SA 3.0