Using powershell to fill a web text box and press a button - powershell

I am trying to use powershell to automate opening IE and placing a value inside a text box and pressing a button.
Add-Type -AssemblyName Microsoft.VisualBasic
cls
$orderID = "text box entry"
$smoURL = "URL"
$iterator = 1;
Write-Host ("ID = $orderID")
$orderIDTextBox = "txtOrderID"
$searchButton = "btnSearch"
$IE = New-Object -ComObject InternetExplorer.Application
$ie.visible = $true
$ie.navigate("$smoURL")
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 2000}
Start-Sleep -Seconds 3
$ie.Document.getElementById("$orderIDTextBox").value = "$orderID"
$Submit = $ie.Document.getElementsByTagName("$btnSearch")
$Submit.click()
I've got the site to open but when I try populate the text field I get this error message:
This is the inspect element on the webpage.

Related

How to Display output to PowerShell form textbox data from PowerShell output

I had wrote a powershell script to ping a ip address and I want to display a ping result to powershell form textboxt. I tried the below scripts below script. Script is working but not clear output.
Herewith i had attached the powershell output and powershell form textbox outputs.
[PowerShell output][1]
# Load required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create Form to contain elements
$Form = New-Object System.Windows.Forms.Form
# Set Form Titlebar text
$Form.Text = "PING"
# Set size of form
# Size(<length>,<height>) in pixels
$Form.Size = New-Object System.Drawing.Size(600,600)
# Create an Input textbox
$inputBoxui = New-Object System.Windows.Forms.TextBox
$inputBoxui.Location = New-Object System.Drawing.Size(20,50)
$inputBoxui.Size = New-Object System.Drawing.Size(100,20)
# Initialize the textbox inside the Form
$Form.Controls.Add($inputBoxui)
# Create Instruction Label for inputBox
$Labelui = New-Object System.Windows.Forms.Label
$Labelui.Text = "Enter IP Address"
$Labelui.Location = New-Object System.Drawing.Size(20,30)
$Labelui.BackColor = "Transparent"
$Labelui.AutoSize = $true
# Initialize Label
$Form.Controls.Add($Labelui)
# Create an Output textbox, 10 pixels in from Form Boundary and 150 pixels down
# As we want a multiline output set textbox size to 565 px x 200 px
# .Multiline declares the textbox is multi-line
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,250)
$outputBox.Size = New-Object System.Drawing.Size(565,300)
$outputBox.MultiLine = $True
$outputBox.Scrollbars = "Vertical"
# Initialize the textbox inside the Form
$Form.Controls.Add($OutputBox)
# Add a Button which can be used to generate an action from our textboxes
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(20,80)
$Button.Size = New-Object System.Drawing.Size(100,40)
$Button.Text = "PING Start"
# Declare the action to occur when button clicked
$Button.Add_Click( { pingstart } )
# Initialize the button inside the Form
$Form.Controls.Add($Button)
# Create a Function
function pingstart {
$outputBox.Clear()
# Variable to store what user types into Input textbox
$Inputui = $inputBoxui.Text
while ($true) {
$Computer = $Inputui
$Ping = Test-Connection -Count 3 -ComputerName $Computer
ForEach ($Result in $Ping) {
If ($Result.ResponseTime -lt 100) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Green
}
If ( ($Result.ResponseTime -ge 100) -and ($Result.ResponseTime -lt 200) ) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Yellow
}
If ($Result.ResponseTime -ge 200) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Red
}
}
# Assign Result to OutputBox
$outputBox.Text = $Ping
}
}
# Initialize form and show it
# [void] used to suppress other messages generated by Form actions
[void] $Form.ShowDialog()
unrelated but it seems there is a mistake with this
Initialize the textbox inside the Form
$Form.Controls.Add($OutputBox)
caps lock.
$outputBox = New-Object System.Windows.Forms.TextBox

IE Automation with Powershell send ENTER Key

I am doing an IE Automation with ServiceNow where there is an option to fill the search data but there is no search button available to use the CLICK method. So I am looking for the method to enter key like {ENTER} or {~} once I filled the search data. But I am in a middle stage of PowerShell scripting and not sure how to use that.
If someone could help me with the method that would be greatly appreciate.
$IE = New-Object -ComObject InternetExplorer.application
$IE.FullScreen = $false
$IE.Visible = $true
$IE.Navigate($ServiceNowURL)
While ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 50
}
$Enter = Read-Host 'To continue press ENTER'
#Enter
$Search = $IE.Document.IHTMLDocument3_getElementsByTagName('input') | ? {$_.id -eq 'sysparm_search'}
$EnterValue = $Search.value() = $TicketNumber
First, you need to active IE window and bring it to front using AppActivate, then set focus to the search area using focus(). After that, you can send Enter key using SendKeys.
I use https://www.google.com to search as an example and you can refer to my code sample below. I test it and it works well:
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("https://www.google.com") #change it to your own url
while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$search=$ie.Document.getElementsByName("q")[0] #change it to your own selector
$search.value="PowerShell" #change it to your own search value
Sleep 5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id)
$search.focus()
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");

Tray Icon Context Menu without hidden Form

I've been experimenting with tray icons & context menus in PowerShell for some time. However, i can only get the context menu to work correctly when a Form is called in the same script.
Here is a small example:
Add-Type -AssemblyName "System.Windows.Forms"
$objForm = New-Object System.Windows.Forms.Form
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objContextMenu = New-Object System.Windows.Forms.ContextMenu
$objExitMenuItem = New-Object System.Windows.Forms.MenuItem
$objExitMenuItem.Index = 1
$objExitMenuItem.Text = "Exit"
$objExitMenuItem.add_Click({
$objForm.Close()
$objNotifyIcon.visible = $false
})
$objContextMenu.MenuItems.Add($objExitMenuItem) | Out-Null
$objNotifyIcon.Icon = "$PSScriptRoot\win.ico"
$objNotifyIcon.Text = "Context Menu"
$objNotifyIcon.ContextMenu = $objContextMenu
$objForm.ContextMenu = $objContextMenu
#Enabling Icon in Taskbar
$objNotifyIcon.Visible = $true
#Hiding Form as best as possible
$objForm.Visible = $false
$objForm.WindowState = "minimized"
$objForm.ShowInTaskbar = $false
$objForm.add_Closing({ $objForm.ShowInTaskBar = $False })
$objForm.ShowDialog()
As soon as the Form componets are removed, the Context menu wont work correctly.
Does anyone know why you need this Form to be loaded and is there a way around it?
System.Windows.Forms.ApplicationContext is what you need to use to acheive that.

Cannot close Internet Explorer with Powershell after form submission

I open a page, fill in fields, click submit. The submit does do a redirect. When I try $ie2.quit() it won't close the window. Any ideas?
$ie2 = New-Object -com InternetExplorer.Application
$ie2.visible=$true
$ie2.navigate("http://localhost/useradd.jsp")
start-sleep -seconds 3
$ie2.Document.getElementById("UserId").value = "test1"
$ie2.Document.getElementById("UserName").value = "test1 user"
$ie2.Document.getElementById("UserPassword").value = "12345$"
$submitButton = $ie2.document.getElementsByTagName("input")
Foreach($element in $submitButton )
{
#look for this field by value this is the field(look for screenshot below)
if($element.value -eq "Add"){
Write-Host $element.click()
}
$ie2.quit()
Try to add something like while ($ie2.Busy) { Start-Sleep -Seconds 1 } before the $ie2.quit() Statement to make sure, the COM Object is repsonsive.

Powershell script to do web UI Automation not working

I have this script to launch IE, navigate to a page and search for some text:
$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
$ie.Navigate("http://www.google.com")
$doc = $ie.Document
if ($doc -eq $null)
{
Write-Host "The document is null."
return
}
$tb1 = $doc.getElementsByName("q") # a text box
$tb1.value = "search text";
$btn = $doc.getElementsByName("btnG")
$btn.click()
I save this as a ps1 file and run it from the command line... but the document object returned by $ie.Document is always null.
What am I doing wrong?
Also, when I run the script line by line in interpreter mode, the document is returned, but the next line $tb = $doc.getElementsByName("q") errors with this: Property 'Value' cannot be found on this object; make sure it exists and is settable.
How do I set the value of the text box, then?
You need to check if IE was done loading the page before $doc assignment. For example,
while ($ie.busy) {
#Sleep a bit
}
I tried the same code for entering the search text and button click but that did not work. So, ended up modiying your code to
$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
$ie.Navigate("http://www.google.com")
While ($ie.Busy) {
Sleep 2
}
$doc = $ie.Document
$btns = $doc.getElementsByTagName("input")
$SearchText = $btns | ? { $_.Name -eq "q" }
$SearchText.value = "search text"
$SearchButton = $btns | ? { $_.Name -eq "btnG" }
$SearchButton.click()
I believe there are two issues that I can see. First, Ravikahth's suggestion to add the ability to wait for the page to finish loading is important. If you do not wait for the page to load (i.e. $ie.busy -eq $false), then you will not get the full document.
Second, for whatever reason, Google decided to add multiple input fields with the name of "q." You can add a second condition to Ravikanth's query as stated below:
$SearchText = $btns | ? { $_.Name -eq "q" -and $_.Type -eq "text"}