How to click an href hyperlink using PowerShell - 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()

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 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($_) }
}
}

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

Website UN and PW Automation getting into website

What I am trying to do is automate a login for a specific website. The only thing I am having trouble with is figuring out how to make it enter into the website once it populates my UN and PW.
How do I make this script advance into the website?
$username = "XXXX";
$password = "XXX:)";
$loginUrl = "https://example.com";
$iterator = 1;
#initialize browser
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($loginUrl);
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; } #wait for browser idle
($ie.document.getElementsByName("username") |select -first 1).value = $username;
($ie.document.getElementsByName("password") |select -first 1).value = $password;
($ie.document.getElementsByName("login") |select -first 1).click();
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; } #wait for browser idle

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