Selecting List item in a website using Powershell - powershell

I'm looking to select a list item (3M) given in the attached image from a website using powershell. I've tried below but no luck .Please suggest
HTML code
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible=$true
$ie.navigate2('https://www.nseindia.com/companies-listing/corporate-filings-insider-trading')
while($ie.Busy) {Start-Sleep 1}
$dropdown = $ie.Document.getElementsByClassName('dayslisting')
($dropdown | where {$_.innerHTML -like "3M"}).Selected = $true

I think you were really close with your solution. This code worked for me. I put comments inline to explain the couple changes I made.
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible=$true
$ie.navigate2('https://www.nseindia.com/companies-listing/corporate-filings-insider-trading')
while($ie.Busy) {Start-Sleep 1}
$dropdown = $ie.Document.body.getElementsByClassName('dayslisting') # You needed to select .body here to use getElementsByClassName
$3m = ($dropdown[0].all | where {$_.innerHTML -like "3M"}) # Dropdown is a list because classes can be assigned to more than one element. I'm getting all elements of the first list, but you could do something better if you wanted.
$3m.click() # Click the "3M" button.

The below code can also help to click on the 3M option on the list.
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible=$true
$ie.navigate2('https://www.nseindia.com/companies-listing/corporate-filings-insider-trading')
while($ie.Busy) {Start-Sleep 1}
$dropdown = $ie.Document.getElementsByClassName('dayslisting')
$link = $ie.Document.links | Where-Object {$_.outerText -eq '3M'}
$link.click()

Related

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}");

PowerShell Button not clicking

I want to click this Button: (Google.com)
<input value="Google-Suche" aria-label="Google-Suche" name="btnK" type="submit" jsaction="sf.chk">
This is my code:
$ie = New-Object -Com InternetExplorer.Application
$ie.Visible = $true
$ie.navigate("http://google.com")
Start-Sleep 5
$ie.Document.IHTMLDocument3_getElementById("lst-ib").value = $Keywords
$Link = $ie.Document.getElementsByTagName("input") | Where-Object {$_.name -eq "btnK"}
$Link.Click()
If I run it everything works fine, but it doesn't press the Button. However if I manually execute $Link.Click() it works.
Any idea how to fix this? Or has my Code some flaws? I am getting no Error Messages. I already trying putting a Start-Sleep 10 before clicking the Button, but that doesn't work either.
PS: I use Google just for testing. This Code will be for another Site, but I can't Access it at the moment.
You should sleep while ie is busy. This works for me:
$ie = New-Object -Com InternetExplorer.Application
$ie.Visible = $true
$ie.navigate("http://google.com")
while($ie.Busy) { Start-Sleep -Milliseconds 100 }
$ie.Document.IHTMLDocument3_getElementById("lst-ib").value = "cheesecake"
$Link = $ie.Document.getElementsByTagName("input") | Where-Object {$_.name -eq "btnK"}
$Link.Click()

Powershell Can not click the site on Google

Hello dear friends and forum leaders
My friends have a script that does not work properly, thank you for guiding me:
This script opens an Internet Explorer browser and Google and serach "Download Music". I want to go to this page to find the nicmusic site in first page search result and click on the site to enter the site but it does not happen !
$IE=new-object -com internetexplorer.application
$IE.navigate("https://www.google.com/search?ei=j-KLWrGyI5G2gQbN9KOQAg&q=%D8%AF%D8%A7%D9%86%D9%84%D9%88%D8%AF+%D9%85%D9%88%D8%B2%DB%8C%DA%A9&oq=%D8%AF%D8%A7%D9%86%D9%84%D9%88%D8%AF+%D9%85%D9%88%D8%B2%DB%8C%DA%A9&gs_l=psy-ab.3..0l10.509762.515959.0.516306.23.19.1.0.0.0.358.2")
$IE.visible=$true
while($ie.busy) {sleep 5}
$currentDocument = $ieObject.Document
$Link=$ie.Document.getElementsByTagName("input") | where-object {$_.className -eq "continue"}
$Link.click()
Thank you for your guidance
Sorry, English is not my first language
This works, though it's dependent on the InnerHTML property:
$IE = new-object -com internetexplorer.application
$IE.navigate("https://www.google.com/search?ei=j-KLWrGyI5G2gQbN9KOQAg&q=%D8%AF%D8%A7%D9%86%D9%84%D9%88%D8%AF+%D9%85%D9%88%D8%B2%DB%8C%DA%A9&oq=%D8%AF%D8%A7%D9%86%D9%84%D9%88%D8%AF+%D9%85%D9%88%D8%B2%DB%8C%DA%A9&gs_l=psy-ab.3..0l10.509762.515959.0.516306.23.19.1.0.0.0.358.2")
$IE.visible=$true
while($ie.busy) {sleep 5}
$Link = $IE.Document.getElementsByTagName("span") | ? {$_.InnerHTML -eq "دانلود آهنگ جدید | دانلود موزیک"}
$Link.click()

Download an Excel document from website with Powershell

recently I began to learn PowerShell to automate my job tasks.
So I want to access a web page and click on a button that download automatically an Excel file. This is the button that I want to click on:
<div class="NormalButton">
<a class="ActiveLink" title="Excel" alt="Excel" onclick="$find('ctl32').exportReport('EXCELOPENXML');" href="javascript:void(0)" style="padding:3px 8px 3px 8px;display:block;white-space:nowrap;text-decoration:none;">Excel</a>
</div>
This would be my PowerShell script:
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("http://test.test/")
$ie.Visible = $true
$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.onclick -eq "$find('ctl32').exportReport('EXCELOPENXML');"}
$link.click()
If I try to run it from the console I receive the error "You cannot call a method on a null-valued expression."
If its useful I'm using PowerShell 4.0 and the webpage has a delay until the report is loaded.
I have completed it by the following code:
[void][System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$url = "https://webpage.com"
$ie = New-Object -com internetexplorer.application
$ie.navigate($url)
$ie.StatusBar = $false
$ie.ToolBar = $false
$ie.visible = $true
#Get Excel
Start-Sleep -s 40
$btnExcel = $ie.Document.links | where-object { $_.outerText -eq 'Excel' -and $_.innerText -eq 'Excel' }
$btnExcel.click()
# Get Internet Explorer Focus
Start-Sleep -s 5
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("{F6}");
[System.Windows.Forms.SendKeys]::SendWait("{TAB}");
[System.Windows.Forms.SendKeys]::SendWait(" ");
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait("$file");
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}");
# Get Internet Explorer Focus
Start-Sleep -s 1
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("^{F4}");
Thank you all for you time :)
I Think the Your problem is because you are using Double Quotes which powershell threat it like variable and Try to expand it, so try to change it to Single quote, from:
$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.onclick -eq "$find('ctl32').exportReport('EXCELOPENXML');"}
to:
$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.onclick -eq '$find('ctl32').exportReport('EXCELOPENXML');'}
Also, you can change the -eq to -match and take just a portion of it like:
$_.onclick -match '$find('ctl32').exportReport'
For more information see: About Quoting Rules

Powershell IE login automation unable to find the element by ID

Can someone knowledgeable in powershell tell me why I can't find the username input field using its ID "user_name"? I have tried this on other websites and it has worked but for this one it just refuses to find it. I also tried the IHTMLDocument3_getElementsByName method and also IHTMLDocument3_getElementsByTagName looking for things like "input" or even "div". Some div classes pop up but so many are missing there and I have no idea why its missing. Please show me the light.
#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"
$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$ie.navigate($URL)
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
$ie.Document.IHTMLDocument3_getElementByID($IDuserName).Value = $userName
Below is the HTML line for the Username input field
<input name="user_name" id="user_name" type="text" class="form-control" value="" autocomplete="off">
The page is built with loads of javascript. If you try opening the page without javascript enabled in your browser you won't even see the login form. this seems to be the problem for IE/PowerShell to find the form and its elements.
However you could bring the Internet Explorer window into foreground and interact with the page using Systems.Windows.Forms like this:
#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"
$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($ie.Name)
Start-Sleep -Milliseconds 500
$ie.navigate($url)
While ($ie.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('username');
[System.Windows.Forms.SendKeys]::SendWait('{TAB}');
[System.Windows.Forms.SendKeys]::SendWait('password');
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}');
May I ask what the purpose of the script you are building is?
Best regards
Guenther