1

I want to automate a task at my work where i need to fill a form in one of our intranet sites. Basically i need to call the site, fill a specific textbox and click the save button. I am trying first to test the code on Google website but it is not working, although i am copying most of it from various sources.

The code mostly works fine. I am able to open Internet explorer, navigate to google, fill the search textbox, but i am stuck when clicking the button.

$Url

$Textbox


$Url = “www.google.com” 

$Textbox=”test” 

$IE = New-Object -com internetexplorer.application; 

$IE.visible = $true 

$IE.navigate($url) 

while ($IE.Busy -eq $true) 

 { 

 Start-Sleep -Milliseconds 2000 

 } 

$IE.Document.getElementsByname(“q”)[0].value = $Textbox 

$IE.Document.getElementsByname(“btnk”)[0].Click()

while ($IE.Busy -eq $true) 

{ 

Start-Sleep -Milliseconds 2000 

}

The error message i keep getting is this:

You cannot call a method on a null-valued expression. At line:17 char:1 + $IE.Document.getElementsByname(“btnk”)[0].Click() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

enter image description here

3
  • I think one of the the real problems here is that you do not use straight quotes, but curly 'smart-quotes' in your code. Commented Oct 15, 2019 at 14:04
  • Thank you Theo, I am not familiar with PowerShell yet so i don't really know the difference between using straight quotes and curly ones :) Commented Oct 16, 2019 at 8:21
  • That's not hard to see.. Have a look at this “q” versus this "q" Commented Oct 16, 2019 at 9:56

3 Answers 3

1

getElementsByname name parameter is case sensitive.

The name of the Search element is btnK and not btnk.

Change to:

$IE.Document.getElementsByname("btnK")[0].Click()
Sign up to request clarification or add additional context in comments.

1 Comment

Yes oh Yes...... i updated the name as you said and it worked straight away. Thank you and god bless you.
1

I have reproduced the problem on my machine, it seems that this behavior is related to the IE browser protected mode. Before turning on the protected mode, it will show this behavior.

To solve this issue, please turn on the Protected Mode (in my machine, I enabled the protected mode in different zones. The screenshot like this).

Besides, you could also run the PowerShell as admin.

The PowerShell script as below:

$Url
$Textbox
$Url = "www.google.com" 
$Textbox="test" 
$IE = New-Object -com internetexplorer.application; 
$IE.visible = $true 
$IE.navigate($url) 
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000  } 
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("q")[0].value = $Textbox
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("btnK")[0].Click()
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000 }

1 Comment

I ran the code without turning on the protected mode and it worked after i changed the button name from "btnk" to "btnK" ..... but your code raised my attention to include 1 second between entering the text and clicking the button.... Thank you
0

I've always used:

$Button_Name.RaiseEvent((New-Object -TypeName System.Windows.RoutedEventArgs $([System.Windows.Controls.Button]::ClickEvent)))

Because it works for me and I'm too lazy to look for anything else.

1 Comment

It seems too complex to me :) but thank you.... i will surly try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.