Resolve Powershell Script to Click On Google Result - powershell

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:

Related

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: 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
}

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

Powershell website automating button click on login

So I'm new to powershell. I've built a few scripts for fun but got stuck on one that I don't seem to be able to figure out. I'm trying to automate the clicking of the "Continue" button but don't know what to do. I have tried everything I can think of. Any ideas?
$username='username'
$password='password'
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("https://www.ksl.com/public/member/signin?login_forward=%2F")
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
$usernamefield = $ie.Document.getElementByID('memberemail')
$usernamefield.value = $username
$passwordfield = $ie.Document.getElementByID('memberpassword')
$passwordfield.value = $password
$Link=$ie.Document.getElementsByTagName("input") | where-object {$_.type -eq "continue"}
$Link.click()
The problem is that the object's Type is Image, not continue. The ClassName is continue. Try this line in that code and see if that works for you:
$Link=$ie.Document.getElementsByTagName("input") | where-object {$_.className -eq "continue"}
Try
$ie.Document.getElementByID('dado_form_3').submit()