Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - IE Object, Set value error

I want to create a script with PowerShell to check the flight status. But it´s not possible for me to set textbox value.

How can I fix it? Thank you in advance!

Error: Property 'value' cannot be found on this object; make sure it exists and is settable.

Code:

#Flight Number (Only Lufthansa)
#For example flight number
[string]$flightNumber = "LH 3102"

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.lufthansa.com/de/de/Ankunft-und-Abflug")
$ie.visible = $true
sleep 5
#while ($ie.busy) {sleep -milliseconds 50}
while($ie.ReadyState -ne 4) {start-sleep -m 100}     

$ie.document.getElementsByName("flightNumber").value = $flightNumber
#Error
$ie.document.getElementsByName("flightNumber").IHTMLInputTextElement_value = $flightNumber
#Error
$ie.document.getElementsByName("flightNumber").IHTMLInputElement_value = $flightNumber
#Error
like image 292
LaPhi Avatar asked Jan 28 '12 16:01

LaPhi


2 Answers

Try this:

$ie.document.getElementByID("ns_7_CO19VHUC6FFPF0I5O4OBCT2OE4_flightNumber").value = $flightNumber

This works for me. I found the ID by looking at the source code through my browser.

I don't know why getElementsByName doesn't work. Perhaps someone else can shed some light on this?


Edit:

Okay, I figured it out. getElementsByName returns a collection. You have to iterate through the collection to set the value. So your code would look like this:

#Flight Number (Only Lufthansa)
#For example flight number
[string]$flightNumber = "LH 3102"

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.lufthansa.com/de/de/Ankunft-und-Abflug")
$ie.visible = $true
sleep 5
#while ($ie.busy) {sleep -milliseconds 50}
while($ie.ReadyState -ne 4) {start-sleep -m 100}     

$elements = $ie.document.getElementsByName("flightNumber")

Foreach($element in $elements)
    {
        $element.value = $flightnumber
    } 

This worked for me.

like image 79
Crownedjitter Avatar answered Oct 07 '22 16:10

Crownedjitter


Very simple , just look for the tag name:

$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true

$ie.navigate("http://www.lufthansa.com/de/de/Ankunft-und-Abflug") 
while($ie.ReadyState -ne 4) {start-sleep -m 100} 

# here is where the magic happens
$termsField = $ie.document.getElementsByName("routeDepartureStationName")
@($termsField)[0].value ="powershell"

enter image description here

like image 25
grepit Avatar answered Oct 07 '22 16:10

grepit