I'm trying to click in link, but nothing happens. Help please! Where did I make a mistake?
My OS ,PS and IE versions:
PS Z:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
PS Z:\> gcim Win32_OperatingSystem | select caption,CSDVersion,version,OSArchitecture
caption CSDVersion version OSArchitecture
------- ---------- ------- --------------
Microsoft Windows 7 Профессиональная Service Pack 1 6.1.7601 64-bit
PS Z:\> (Get-ItemProperty -Path 'HKLM:SOFTWARE\Microsoft\Internet Explorer').svcVersion
11.0.9600.18738
I can not click on this image
Here is an output of the powershell commands which shows the element I need to click:
PS Z:\> $ie.document.getElementById("ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert").outerHTML
<input name="ctl00$CPH$NP2$gvUserNotepads$ctl06$ibInsert" id="ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert" style="border
-width: 0px; background-color: transparent;" onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptio
ns("ctl00$CPH$NP2$gvUserNotepads$ctl06$ibInsert", "", true, "Insert", "", false, false))' type="image" alt="Добавить" sr
c="../../../Images/b_add.gif">
PS Z:\> $ie.document.getElementById("ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert") | select Type, Name, ID
type name id
---- ---- --
image ctl00$CPH$NP2$gvUserNotepads$ctl06$ibInsert ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert
Here are my attempts:
1) $Link = $ie.document.getElementById("ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert") | where-object {$_.type -eq "image"}
$Link.click();
2) $ie.document.getElementById("ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert").click()
Here is the powershell code.You can test it yourself (login/pass are real):
$userlogin = "all"
$userpasswd = "1234567"
$loginpage = "http://aszp.gaz-is.ru/aszp2/Autorization/ASZPLogin.aspx?ReturnUrl=%2faszp2%2fDefault.aspx"
$notepad = "http://aszp.gaz-is.ru/aszp2/ARMs/CreateRequest/Notepads/Notepads.aspx"
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $false
$ie.Navigate2($loginpage)
While ($ie.Busy) { Start-Sleep -Milliseconds 400 }
$forms = $ie.Document.forms.namedItem("form1")
$forms.item("Login1_UserName").value= $userlogin
$forms.item("Login1_Password").value = $userpasswd
$ie.document.getElementById("Login1_Button1").click()
While ($ie.Busy) { Start-Sleep -Milliseconds 400 }
$ie.navigate($notepad)
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }
$forms2 = $ie.Document.forms.namedItem("aspnetForm")
$forms2.item("ctl00_CPH_NP2_gvUserNotepads_ctl06_txtTEI1").value= "Petrov"
$forms2.item("ctl00_CPH_NP2_gvUserNotepads_ctl06_txtTEI2").value = "Ivan"
$forms2.item("ctl00_CPH_NP2_gvUserNotepads_ctl06_txtTEI3").value = "Nikolaevich"
$forms2.item("ctl00_CPH_NP2_gvUserNotepads_ctl06_txtTEI6").value = "Driver"
$forms2.item("ctl00_CPH_NP2_gvUserNotepads_ctl06_txtTEI8").value = "photo_321"
$ie.document.getElementById("ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert").click()
While ($ie.Busy) { Start-Sleep -Milliseconds 400 }
$ie.Visible = $true
Here is the link to my server where the simplified code is. Everything works on it
Example powershell code for my link:
$loginpage = "https://allovergeorgia.com/vk/form/input.html"
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate2($loginpage)
While ($ie.Busy) { Start-Sleep -Milliseconds 400 }
$ie.document.getElementById("ctl00_CPH_NP2_gvUserNotepads_ctl06_ibInsert").click()
$ie.Document.Body.InnerText
I found the reason.
It was necessary to look at the full picture. Sending the form was allowed only when all required fields was filled in.
I forgot about the phone number field: $forms2.item("ctl00_CPH_NP2_gvUserNotepads_ctl06_txtTEIP7_txtEAEBDD").value = "911"
After it was added to the code, everything was fine and the code worked))
Related
The text is an html file. The line I am interested in looks something like:
<td>INC1234</td><td>INC1235</td><td>INC1236</td>
The INC numbers are different from file to file. I'd like to parse through the line by saying something like:
if like <td>INC, then concatenate '<td><a href="https://www.website.com/=' + INC# + '>"
To give an output like:
<td><a href="https://www.website.com/=INC1234>INC1234</a></td><td><a href="https://www.website.com/=INC1235>INC1235</a></td><td><a href="https://www.website.com/=INC1236>INC1236</a></td>"
EDIT1: Ok, if I do something like:
$parse = (-split (Get-Content -Raw C:\Temp\report.txt) -match '<td>INC')
$parse
It will find the characters, but it will return the entire line rather than looking for more that match the 'INC'. Presumably because they all reside on the same line with no spaces.
EDIT2: Maybe this will help. What I'm doing is using PowerShell to write SQL commands, send it to our SQL Server, return the data and use PSWriteHTML to build the report which works fantastic. But I am wanting the first column, which is the ticket number (eg. INC1234) to be a link to the ticket it reads.
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$ReportName
)
## Build the query box
function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
## Create the Label.
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.AutoSize = $true
$label.Text = $Message
## Create the TextBox used to capture the user's text.
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox.Size = New-Object System.Drawing.Size(575,200)
$textBox.AcceptsReturn = $true
$textBox.AcceptsTab = $false
$textBox.Multiline = $true
$textBox.ScrollBars = 'Both'
$textBox.Text = $DefaultText
## Create the OK button.
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Size(415,250)
$okButton.Size = New-Object System.Drawing.Size(75,25)
$okButton.Text = "OK"
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
## Create the Cancel button.
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(510,250)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Cancel"
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
## Create the form.
$form = New-Object System.Windows.Forms.Form
$form.Text = $WindowTitle
$form.Size = New-Object System.Drawing.Size(610,320)
$form.FormBorderStyle = 'FixedSingle'
$form.StartPosition = "CenterScreen"
$form.AutoSizeMode = 'GrowAndShrink'
$form.Topmost = $True
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.ShowInTaskbar = $true
## Add all of the controls to the form.
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($okButton)
$form.Controls.Add($cancelButton)
## Initialize and show the form.
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() > $null # Trash the text of the button that was clicked.
## Return the text that the user entered.
return $form.Tag
}
## Prompt the SQL Query Box
$Query = Read-MultiLineInputBoxDialog -Message "Enter SQL Query Here" -WindowTitle "SQL Query" -DefaultText "SELECT FROM"
if ($Query -eq $null) { Break }
else { Write-Host "You entered the following text: $Query" }
## Pass query to SQL Server
$Pass = Invoke-Sqlcmd -Query $Query -ServerInstance "MY-SERVER-INSTANCE" -Username "USERNAME" -Password "PASSWORD"
## Output the report and save to the network under the specified name
New-HTML {
New-HTMLTable -EnableColumnReorder -DisableInfo -DataTable $Pass -ExcludeProperty "RowError", "RowState", "Table", `
"ItemArray", "HasErrors" -HideFooter -PagingLength 25 -SearchBuilder
New-HTMLTableStyle -FontFamily Calibri -FontSize 15 -FontStyle normal -TextAlign center -TextColor "#0a0a0a"
New-HTMLTableStyle -FontFamily Calibri -BackgroundColor "#fffdb5" -FontSize 15px -TextColor "#0a0a0a" -TextAlign center -Type RowHover
} -ShowHTML -FilePath "\\Server\$ReportName.html" -Online
The report looks something like:
Ticket: Description:
----------------------------
INC1234 Broken Monitor
INC1235 No Sound
The HTML that PSWriteHTML builds throws all of the ticket numbers on one line so I would like to edit that HTML with the <a href=""> tag to dynamically create links for each ticket # mainly because I don't know how to do it in PS and can't seem to find a good answer through Google - which is why I came here.
Although not familiar with PsWriteHtml, I guess you could simply change the Ticket properties in the array you receive in $Pass:
## Pass query to SQL Server
$Pass = Invoke-Sqlcmd -Query $Query -ServerInstance "MY-SERVER-INSTANCE" -Username "USERNAME" -Password "PASSWORD"
# convert all Tickets into hyperlinks
foreach ($item in $Pass) {
$item.Ticket = '<a href="https://www.website.com/={0}>{0}</a>'-f $item.Ticket
}
Then the rest of your code
## Output the report and save to the network under the specified name
New-HTML {...}
I've been trying to create a powershell script to automate something in my router's website but I can't really navigate through the sites frames and framesets, any help would be greatly appreciated!
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$ie.Navigate("http://192.168.1.1")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('userName')
$usernamefield.value = "admin"
$passwordfield = $ie.document.getElementByID('pcPassword')
$passwordfield.value = "admin"
$Link = $ie.document.getElementByID('loginBtn')
$Link.click()
$Frame1a = $ie.document.getElementByID("topFrame")
$Frame2a = $Frame1a.contentWindow
$Frame3a = $Frame2a.document
$Frame1b = $Frame3a.getElementByID('bottomLeftFrame')
$Frame2b = $Frame1b.contentWindow
$Frame3b = $Frame2b.document
$thing = $Frame3b.getElementsByTagName('a') | where-object {$_.innerText -eq 'Wireless'}
This is the code I have so far, but when it gets to $Frame1b = $Frame3a.getElementByID('bottomLeftFrame') I get an error message saying: "You cannot call a method on a null-valued expression."
After I get this frame thing to work, I'd need it to click a hyperlink, if anyone knows how to do that please tell me, I've tried before but it didn't work. I've also tried different languages before, python, VBS, but I got the furthest with powershell.
Thanks for the attention!
I had similar issue in the past. I solved it like this:
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$ie.Navigate("http://192.168.1.1")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('userName')
$usernamefield.value = "admin"
$passwordfield = $ie.document.getElementByID('pcPassword')
$passwordfield.value = "admin"
$Link = $ie.document.getElementByID('loginBtn')
$Link.click()
start-Sleep -Milliseconds 500
#Click Wireless
$oFrameLeft=$ie.document.parentwindow.frames['bottomLeftFrame'].document
$oWireless = $oFrameLeft.IHTMLDocument3_getElementsByTagName('a') | where-object {$_.innerText -eq 'Wireless'}
$oWireless.click()
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
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()
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 }
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.
I have a GUI and it calls a function depending on the button pressed. I would like for the output of the function to show in the powershell command window when I run the GUI. The code below contains 5 buttons, when I run the powershell script and click on any of the 5 buttons, nothing happens and it just hangs, until i close out of it.
# This is code for the GUI ▼
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '406,414'
$Form.text = "Post DC Patching Checker"
$Form.TopMost = $false
$Check_NetLogon = New-Object system.Windows.Forms.Button
$Check_NetLogon.text = "Check Netlogon"
$Check_NetLogon.width = 340
$Check_NetLogon.height = 50
$Check_NetLogon.location = New-Object System.Drawing.Point(15,17)
$Check_NetLogon.Font = 'Microsoft Sans Serif,10'
$Ping = New-Object system.Windows.Forms.Button
$Ping.text = "Ping Servers / Workstations"
$Ping.width = 340
$Ping.height = 50
$Ping.location = New-Object System.Drawing.Point(16,97)
$Ping.Font = 'Microsoft Sans Serif,10'
$ShowReplication = New-Object system.Windows.Forms.Button
$ShowReplication.text = "Show Replication"
$ShowReplication.width = 340
$ShowReplication.height = 50
$ShowReplication.location = New-Object System.Drawing.Point(16,183)
$ShowReplication.Font = 'Microsoft Sans Serif,10'
$DiskSpace = New-Object system.Windows.Forms.Button
$DiskSpace.text = "Disk Space"
$DiskSpace.width = 340
$DiskSpace.height = 50
$DiskSpace.location = New-Object System.Drawing.Point(15,267)
$DiskSpace.Font = 'Microsoft Sans Serif,10'
$CheckDNSsuffix = New-Object system.Windows.Forms.Button
$CheckDNSsuffix.text = "Check IP Configuration"
$CheckDNSsuffix.width = 340
$CheckDNSsuffix.height = 50
$CheckDNSsuffix.location = New-Object System.Drawing.Point(17,350)
$CheckDNSsuffix.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($Check_NetLogon,$Ping,$ShowReplication,$DiskSpace,$CheckDNSsuffix))
$Check_NetLogon.Add_Click({ CheckNetLogon })
$Ping.Add_Click({ PingServersAndWorkstations })
$ShowReplication.Add_Click({ ShowReplicationOnServers })
$DiskSpace.Add_Click({ ShowDiskSpace })
$CheckDNSsuffix.Add_Click({ ShowIPconfig })
# This is code for the GUI ▲
# Check the netlogon service ▼
function CheckNetLogon {
$netLogon =Get-Service -DisplayName netlogon
if ($netLogon.Status -eq "Running"){
$netLogon.DisplayName + 'Service is running already'}
}
# Check the netlogon service ▲
# Ping's several workstations and servers ▼
function PingServersAndWorkstations {
ping test2
ping test3
ping test4
ping test5
}
# Ping's several workstations and servers ▲
# Shows replication ▼
function ShowReplicationOnServers {
repadmin /showrepl
}
# Shows replication ▲
# Shows disk space ▼
function ShowDiskSpace {
Get-WmiObject -Class Win32_logicaldisk |
Select-Object -Property DeviceID, DriveType, VolumeName,
#{L='FreeSpaceGB';E={"{0:N2}" -f ($_.FreeSpace /1GB)}}
}
# Shows replication ▲
# Shows IP config ▼
function ShowIPconfig {
ipconfig
}
# Shows IP config ▲
[void]$Form.ShowDialog()
Add-Type -AssemblyName System.Windows.Forms
$formTest = New-Object system.Windows.Forms.Form
$formTest.Size = '406,414'
$formTest.StartPosition = 'CenterScreen'
$formTest.text = "Post DC Patching Checker"
$buttonPing = New-Object system.Windows.Forms.Button
$formTest.Controls.Add($buttonPing)
$buttonPing.text = "Ping Servers / Workstations"
$buttonPing.Size = '340,50'
$buttonPing.location = '16, 97'
$buttonPing.Font = 'Microsoft Sans Serif,10'
$buttonPing.Add_Click({
$this.Enabled = $false
'google.com', 'test1', 'test2' |
ForEach-Object{
Try{
Test-Connection $_ -Count 1 -ErrorAction Stop|
Out-String |
Write-Host -ForegroundColor green
}
Catch{
Write-Host $_ -fore Red
}
}
$this.Enabled = $true
})
$formTest.ShowDialog()
I want to open next page in Powershell script by selecting first option in Auto-complete.
For example. When I open www.healthkartplus.com and type "Sporanox (100mg)". I want to select first option in Auto-complete.
I do not know how to select option from auto-complete so what I am trying is simply get first link on next page.
$ie = New-Object -COMObject InternetExplorer.Application
$ie.visible = $true
$site = $ie.Navigate("https://www.healthkartplus.com/search/all?name=Sporanox (100mg)")
$ie.ReadyState
while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }
$link = $null
$link = $ie.Document.get_links() | where-object {$_.innerText -eq "Sporanox (100mg)"}
$link.click()