Select IE window using HWND - powershell

Wondering if it is possible to select an IE window based on the HWND property (or similar). My script clicks a link which opens a new page in a separate window and I would like to be able to work with this new window.
Here is my code:
$ie.Navigate("https://chaseloanmanager.chase.com/Chaselock/ViewOnlineGuide.aspx") # opens page in new window
while ($ie.Busy -eq $true){Start-Sleep -Seconds 2}
$childWindow = Get-Process | Where-Object {($_.ProcessName -eq 'iexplore')} | Get-ChildWindow | Where-Object {$_.ChildTitle -match 'Lending'}
$childWindow.MainWindowHandle # gets HWND of new window
$shell = New-Object -ComObject Shell.Application
$ie3 = $shell.Windows() | Where-Object {$_.HWND -match $childWindow.MainWindowHandle} # does not find window

You can get the IE comobject by searching through Windows() in Shell.Application.
$shell = New-Object -ComObject Shell.Application
$ie = $shell.Windows() | Where-Object { $_.LocationURL -match 'Lending' }
#$ie = $shell.Windows() | Where-Object { $_.HWND -eq 2951084 }
$ie.Navigate("http://www.stackoverflow.com")

Related

Resolve Powershell Script to Click On Google Result

i have powershell script to find a website on google result and click on website link
$IE = new-object -com internetexplorer.application
$IE.navigate("https://www.google.com/search?q=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B2%D8%A7%D8%B1%DB%8C+%D9%81%D8%A7%D8%B1%D8%B3&oq=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B0%D8%A7%D8%B1%DB%8C")
$IE.visible=$true
while ($IE.busy) {sleep 10}
$Link = ($html.Links |Where-Object { $_.class -eq 'https://khabarfarsi.com' }) |Select-Object -ExpandProperty href
$Link = ($HTML.ParsedHtml.getElementsByTagName("a") | Where {$_.className -eq 'https://khabarfarsi.com'}).InnerHTML
$Link = #($IE.Document.getElementsByTagName("a") | ? {$_.InnerHTML -like 'https://khabarfarsi.com'})[0]
if ($Link -eq $null){ $Link = $IE.Document.getElementsByTagName("a") | ? {$_.InnerHTML -like 'https://khabarfarsi.com'} }
if ($Link -eq $null){$ie.quit(); Break}
$Link.click()
I have a number of beginner trainees who often have trouble doing this. I want to create a script that they don't need to do manually.
Thank you for accompanying me in doing this
Thanks
Regards
Here is a working script that can find and click the link.
$ie = new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate("https://www.google.com/search?q=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B2%D8%A7%D8%B1%DB%8C+%D9%81%D8%A7%D8%B1%D8%B3&oq=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B0%D8%A7%D8%B1%DB%8C")
while($ie.busy) {sleep 1}
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.href -eq 'https://khabarfarsi.com/w/farsnews.com'}
$link.click()
Output:

Powershell: Unable to click "Google Search" after entering search string

I wrote the following snippet to open Internet explorer and search for a particular string. However i am unable to click the "Google Search" button to view the search results.
$IE=new-object -com internetexplorer.application
$site= "www.google.com"
$IE.navigate2($site)
$IE.visible=$true
$search_string= "Sachin Tendulkar"
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$IE.document.getElementById("lst-ib").value= "$search_string"
$IE.document.getElementsByTagName("input") | Where-Object { $_.Type -eq "submit" -and $_.name -eq "btnK" } | ForEach-Object { $_.Click() };
I have even tried the following snippets to try click the "Google Search" button.
$submitButton = $ie.document.getElementsByTagName("input")
Foreach($element in $submitButton )
{
if($element.type -eq "submit" -and $element.value -eq "Google Search"){
Write-Host $element[0].click()
}
}
and the below one method too.
$btn = $ie.Document.getElementsByTagName("input") | Where-Object {$_.Type -eq "submit" -and $_.name -eq "btnK"}
$btn.click();
I need help getting this done.
why not open the search directly with:
$IE=new-object -com internetexplorer.application
$site= "www.google.com"
$search_string= "Sachin Tendulkar"
$url = "//" + $site + "/search?q=" + [System.Web.HttpUtility]::UrlEncode($search_string)
$IE.navigate2($url)
$IE.visible=$true
so you get the result without clicking the submit button

Use an existing instance of IE unless it contains a specified string

I am using this code to determine whether to use an existing or new instance of IE:
$newInstance = $false
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
$newInstance = $false
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}
# Elsewhere, I open an array of sites depending on what tabs are already open
foreach ($tab in (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }) {
if ($tab.LocationURL.Contains("~"))
{ $search = $true; break }
}
If a tab is open that has specified text in its title, how can I ignore that instance of IE, and use a second instance to open an array of sites in? I have tried this:
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" -and $_.LocationName -like ""}
if ($null -ne $ie) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
$newInstance = $false
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}
Not sure if this is what you're looking for?
$searchString = "something"
$ie = (New-Object -COM 'Shell.Application').Windows() | where {$_.Name -eq 'Internet Explorer' -and $_.LocationName -like "*$searchString*"}
if ($null -ne $ie) {
# existing instance found
$newInstance = $false
}
else {
# create new instance
$ie = New-Object -COM 'InternetExplorer.Application'
$newInstance = $true
}
I have got it working; improvements appreciated.
$newInstance = $false
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
if ((New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" -and $_.LocationName -notlike "**" }) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
$newInstance = $false
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}

Open D drive using powershell

I am editing a powershell script (2.0 I believe) that has the following code to open the cd drive:
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() |
Where-Object { $_.Type -eq "CD Drive" } |
foreach { $_.InvokeVerb("Eject") }
The problem is that this script now runs on a computer with multiple optical drives so they all open - I only want the D drive to open. I thought changing the script to the following would work:
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() |
Where-Object { $_.DriveID -eq "D:" } |
foreach { $_.InvokeVerb("Eject") }
but it did not (none of the drives opened). How can I only open the D drive?
The issue you're having is that the result of $sh.Namespace(17).Items() - at least for me - doesn't include a property called DriveID. It does include "Path" though. Try the below.
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() | ? {$_.Path -eq "D:\"} | % { $_.InvokeVerb("Eject") }
Try filtering on the Path property.
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() | Where-Object { $_.Path -eq "D:\" } | ForEach-Object { $_.InvokeVerb("Eject") }
I don't see a 'DriveID' property, so the Where-Object is filtering all objects (i.e. nothing is passed to the ForEach). Try using the 'Path' property instead?
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() |
Where-Object { $_.Path -eq "D:\" } |
foreach { $_.InvokeVerb("Eject") }

Extracting Content from Webpage with ParsedHtml

I've been trying to use the invoke-Webrequest and the "ParsedHtml.getElements"
ParsedHtml.getElementsByTagName("div") | Where{ $_.className -eq 'pricingContainer-priceContainer' } ).innerText
to try to get the value $8.29 but using it on the below code produces no result. What am I doing wrong?
<div class="pricingContainer pricingContainer--grid u-ngFade noCenterTag" ng-class="::{'noCenterTag': !showCenterTag}" ng-if="::featuresEnabled">
<!-- ngIf: ::(product.IsOnSpecial && !product.HideWasSavedPrice) -->
<div class="pricingContainer-priceContainer">
<span class="pricingContainer-priceAmount" ng-class="::specialClass">$8.29</span>
<!-- ngIf: ::product.CupPrice --><span ng-if="::product.CupPrice" class="pricingContainer-priceCup">
$5.19 / 100G
</span><!-- end ngIf: ::product.CupPrice -->
</div>
</div>
By replacing className by class:
($html.getElementsByTagName("span") | Where{ $_.class -eq 'pricingContainer-priceCup' }).innerText
or
($html.getElementsByTagName("div") | Where{ $_.class -eq 'pricingContainer-priceContainer' }).innerText
An example:
$Site = "http://example.com/index.html"
$all = Invoke-WebRequest -URI $Site
# $all contains all informations of the page
$html = [xml]$all.Content
#[xml] is a cast to convert code to xml
$html.getElementsByTagName("div")
You can use automation with IE. You choose a div witch contains the Card and you can get the innerHTML like this:
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate("http://www.example.com/index.html")
$ie.Visible = $true
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 2000; }
$html= $ie.Document.body.getElementsByTagName('div') | Where-Object {$_.className -eq "cardList-cards cardList-isotopeContainer"}
$lines = $html.innerHTML.split("`n")
$prices = $lines | Where-Object { $_ -Match '<span class=\"pricingContainer\-priceAmount\"' }
$prices = $prices | foreach { [regex]::matches($_, '>([0-9.$]*)</span>').Groups[1].Value }
echo $prices
Worked this bad boy out by opening the webpage , wait for correct html to load over the dynamic html then dumps to a txt file to read and search.
$path = "c:\sourcecode.txt"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("blahblahblahblah insert webpage here")
while($ie.ReadyState -ne 4) {start-sleep -s 10}
$ie.Document.body.outerHTML | Out-File -FilePath $path
$pricebf = select-string -path $path -pattern "pricingContainer-priceAmount" | select-object -First 1 | select Line
$Descriptionbf = select-string -path $path -pattern "canOpenDetail --><a title=" | select-object -First 1 | select Line