0

I'm new to Powershell, and I've been struggling with this issue for longer than I care to admit. I am trying to automate the filling out of an intranet webform, and I thought this would be very simple. I've already got the login component worked out, but I am having trouble with completing the "Last Name" text box on the form.

$url = "www.myintranetsite.com"
$lastname="Smith" 
$firstname="John" 
$ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy -eq $true) 
{ 
Start-Sleep -Milliseconds 1000; 
} 
$ie.Document.getElementById("ERM_SEARCH_WRK_ERM_LAST_NAME_LBL").value = 
$lastname 

The error flagging is that I cannot call a method on a null-valued expression. The offender is the "ERM_SEARCH_WRK_ERM_LAST_NAME_LBL"

Any suggestions as to why $lastname would be returning as a null value?

![1]: https://i.stack.imgur.com/Jf8Ht.jpg ![2]: https://i.stack.imgur.com/pE71n.jpg

Ryan
  • 35
  • 1
  • 9

3 Answers3

0

It is not the last name that is the problem. It is the element. Can you do something like this?

$doc = $ie.document
$lastname = $doc.getElementById('ERM_SEARCH_WRK_ERM_LAST_NAME_LBL')
$lastname.Value = "smith"

And use the single quote on element id instead of double

Once you get the $lastname object defined, check its member using | get-member to see if value is an available property

Sid
  • 2,586
  • 1
  • 11
  • 22
  • Hmmm, value doesn't seem to be an available property. – Ryan Sep 27 '17 at 19:54
  • Can I ask if you are using windows 10? because I had a similar code that worked on win7 but fails on win10 on that line. Perhaps it is the version of IE. I have not really tried to debug it. – Sid Sep 27 '17 at 20:00
  • Nope, I'm using Windows 7. 32 bit system. IE 11. – Ryan Sep 27 '17 at 20:12
0

Your issue is your trying to add a value to Label.

Labels dont hold a value.

https://www.w3schools.com/TagS/tag_label.asp

It looks like based on your picture that the label is for ERM_SEARCH_WRK_ERM_LAST_NAME

$url = "www.myintranetsite.com"
$lastname="Smith" 
$firstname="John" 
$ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy -eq $true) 
{ 
Start-Sleep -Milliseconds 1000; 
} 
$ie.Document.getElementById('ERM_SEARCH_WRK_ERM_BO_NAME$542$').value = $lastname
ArcSet
  • 6,518
  • 1
  • 20
  • 34
  • Thanks ArcSet, so how would I fill in the Last Name box with "Smith"? What would that syntax look like? – Ryan Sep 27 '17 at 20:18
  • posted what should be working code, If that doesnt work then post the source of the page so i can find the name of the input – ArcSet Sep 27 '17 at 20:19
  • I appreciate your help, but the Source code is 2000+ lines. Does this help at all?
    – Ryan Sep 27 '17 at 20:31
  • I did, and I got the same message. – Ryan Sep 27 '17 at 20:35
  • You need to find the Input tag...you keep posting the label tag – ArcSet Sep 27 '17 at 20:36
  • – Ryan Sep 27 '17 at 20:43
  • so find the ID for the input and place the ID name into getElementById – ArcSet Sep 27 '17 at 20:53
  • I tried as many different things as I could, and nothing is working. I'm wondering if my company has this blocked for some reason? Or if I lack admin access? I'm not sure what else it could be. – Ryan Sep 28 '17 at 18:08
  • copy and paste exactly what i have, You will notice that the double qoutes " are now single qoutes' – ArcSet Sep 28 '17 at 18:28
  • Ok, I tried it, didn't work. The odd thing is that I can get it to work with several other sites, just not this one. – Ryan Sep 28 '17 at 19:02
  • Yes. Null valued expression on: $ie.Document.getElementById('ERM_SEARCH_WRK_ERM_BO_NAME$542$').value = $lastname – Ryan Sep 28 '17 at 20:06
  • You cannot call a method on a null-valued expression. At line:18 char:1 + $ie.Document.getElementById('ERM_SEARCH_WRK_ERM_BO_NAME$542$').value = $lastname + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull – Ryan Sep 28 '17 at 20:39
0

I face the same error when trying to click on a button:

    $ie = new-object -com "InternetExplorer.Application"
    $ie.navigate("MyURL")
    $ie.visible = $true
    while ($ie.Busy -eq $true){  Start-Sleep -Milliseconds 1000;  }
    $doc = $ie.document
$btn=$doc.getElementByID('ctl00_PageHeader1_ctl06_c_b_Start_c_b_View_c_b_ExportDefaultButton_ExportDefaultButton')
    $btn.click()

the error is: "You cannot call a method on a null-valued expression."

Windows 10 x64

Luca
  • 1,588
  • 2
  • 22
  • 26
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30738950) – Luca Kiebel Jan 07 '22 at 12:36