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

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

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:

How to click an href hyperlink using PowerShell

I am writing a PowerShell script to login to a website and download some reports. I know next to nothing about PowerShell.
I've been able to frankenstein together a script that will login, but I am having trouble clicking the menu buttons. Inspect element shows the button (labeled "abc" in this case) as:
<a class="category-item-text" href="#">abc</a>
My current script looks like:
$ie.Visible= $true # Make it visible
$username="MYUSERNAME"
$password="MYPASSWORD"
$ie.Navigate("https://MY.WEBPAGE/yada/yada/yada")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('LOGIN_FIELD_LABEL')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('PASSWORD_FIELD_LABEL')
$passwordfield.value = "$password"
$Click=$ie.Document.getElementsByTagName("button") | Where-Object {$_.type -eq 'submit'}
$Click.click()
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$ie.Navigate("https://MY.WEBPAGE/yada/yada/REPORTS")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
Any help would be greatly appreciated!
This worked for me. I needed to use getElementsByTagName("a"):
$Click=$ie.Document.getElementsByTagName("a") | Where-Object {$_.href -match 'abc'}
$Click.click()

PowerShell To Retreive HTML from an open tab

I am trying to retrieve links from a dynamic JavaScript-rendered web page using PowerShell.
This is the code I have so far:
# Create IE object and load URL
$ie = New-Object -comobject "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($url)
# Wait for the page to load
while ($ie.Busy -eq $true -Or $ie.ReadyState -ne 4) {Start-Sleep 2}
$Doc = $ie.Document
$divs = $Doc.getElementsByTagName('a')
foreach ($div in $divs){
write-host $div['id'].value
write-host $div['tagName'].value
write-host $div['parentElement'].value
write-host $div['style'].value
write-host $div['document'].value
write-host $div['sourceIndex'].value
write-host $div['offsetLeft'].value
write-host $div['offsetTop'].value
write-host $div['offsetWidth'].value
write-host $div['offsetHeight'].value
write-host $div['offsetParent'].value
write-host $div['innerHTML'].value
write-host $div['innerText'].value
write-host $div['outerHTML'].value
write-host $div['outerText'].value
write-host $div['parentTextEdit'].value
}
However, all of the output is blank lines.
(FYI - if I output just the $div, then I get System.__ComObject
Can anyone explain what I need to do to get the information?
Thank you.
First of all, I would change the names of $divs to $tags and use $tag rather then $div in the foreach loop, because you are not really looking for divs, but <a> tags.
Each $tag has a getAttribute method you should use instead of $tag['attribName'].value.
Your code could then look like this
$ie = New-Object -comobject "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($url)
# Wait for the page to load
while ($ie.Busy -eq $true -Or $ie.ReadyState -ne 4) {Start-Sleep 2}
$Doc = $ie.Document
$tags = $Doc.getElementsByTagName("a")
foreach ($tag in $tags){
Write-Host $tag.getAttribute("id")
Write-Host $tag.getAttribute("tagName")
Write-Host $tag.getAttribute("parentElement")
Write-Host $tag.getAttribute("style")
Write-Host $tag.getAttribute("document")
Write-Host $tag.getAttribute("sourceIndex")
Write-Host $tag.getAttribute("offsetLeft")
Write-Host $tag.getAttribute("offsetTop")
Write-Host $tag.getAttribute("offsetWidth")
Write-Host $tag.getAttribute("offsetHeight")
Write-Host $tag.getAttribute("offsetParent")
Write-Host $tag.getAttribute("innerHTML")
Write-Host $tag.getAttribute("innerText")
Write-Host $tag.getAttribute("outerHTML")
Write-Host $tag.getAttribute("outerText")
Write-Host $tag.getAttribute("parentTextEdit")
}
Also, when finished you should cleanup the COM object you have created:
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($ie) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Edit
to make better output i suggest this:
$Doc = $ie.Document
$tags = $Doc.getElementsByTagName("a")
#$tags = $doc.all.tags("a")
$attribs = #("id", "tagName", "parentElement", "style", "document", "sourceIndex",
"offsetLeft", "offsetTop", "offsetWidth", "offsetHeight", "offsetParent",
"innerHTML", "innerText", "outerHTML", "outerText", "parentTextEdit")
foreach ($tag in $tags){
$attribs | ForEach-Object {
#{ $_ = $tag.getAttribute($_) }
}
}

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
}

Select IE window using HWND

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