Powershell get href from page and assign to variable

I need to assign this href value to a variable

This is what I have
$hrefOT = $ie.Document.getElementsByTagName('a') | ? {$_.innerText -eq 'Daily Employee Overtime Report'}

Open in new window


here is the html code
code
LVL 8
Leo TorresSQL DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leo TorresSQL DeveloperAuthor Commented:
If you need more HTML let me know I will share i posting what I think is most relevant.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
In my very restricted test case this code was sufficient:
$ie.Document.getElementsByTagName("a") | ? { $_.Classname -eq "newreport" } | select -expand href

Open in new window

That, of course, can differ if you have a lot of "newreport" class tags. You can add your InnerText check, or only do that one - it doesn't make a difference in my simple test case.
Do you get anything back? Or what is the issue with your code?
0
Leo TorresSQL DeveloperAuthor Commented:
No did not work I want put the href string in a variable so I can use it
Here is some sample code:
           <span class=spacer>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img border="0" src="/images/interface/arrowrightgreen.gif" alt='' width=10 height=10>
<a class='newReport' href="/reports/cognos/reportParams.jsp?mfrm_id=10613&amp;report_name=DAILY+EMPLOYEE+OVERTIME+DETAIL+REPORT&amp;mfrm_id=10613&amp;uiPathLabel=Daily%20Employee%20Overtime%20Detail%20Report&amp;mfrmUIPathLabel=Daily+Employee+Overtime+Detail+Report&amp;currentID=10613&amp;parentID=216">Daily Employee Overtime Detail Report</a><br> 
           <span class=spacer>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img border="0" src="/images/interface/arrowrightgreen.gif" alt='' width=10 height=10>
<a class='newReport' href="/reports/cognos/reportParams.jsp?mfrm_id=10623&amp;report_name=PAYROLL+CONTROL+TOTALS&amp;mfrm_id=10623&amp;uiPathLabel=Payroll%20Control%20Totals&amp;mfrmUIPathLabel=Payroll+Control+Totals&amp;currentID=10623&amp;parentID=216">Payroll Control Totals</a><br> 
           <span class=spacer>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img border="0" src="/images/interface/arrowrightgreen.gif" alt='' width=10 height=10>
<a class='newReport' href="/reports/cognos/reportParams.jsp?mfrm_id=1080&amp;report_name=AUDIT+TRACKING+REPORT&amp;mfrm_id=1080&amp;uiPathLabel=Audit%20Tracking%20Report&amp;mfrmUIPathLabel=Audit+Tracking+Report&amp;currentID=1080&amp;parentID=216">Audit Tracking Report</a><br> 

Open in new window


from this string I need to assign this value below to a variable
/reports/cognos/reportParams.jsp?mfrm_id=10613&amp;report_name=DAILY+EMPLOYEE+OVERTIME+DETAIL+REPORT&amp;mfrm_id=10613&amp;uiPathLabel=Daily%20Employee%20Overtime%20Detail%20Report&amp;mfrmUIPathLabel=Daily+Employee+Overtime+Detail+Report&amp;currentID=10613&amp;parentID=216

Open in new window


This is the code I tried
$Link = $ie.Document.getElementsByTagName("a") | ? { $_.class -eq "newreport" -and $_.innerText -like '*Overtime Report*' } | select -expand href

Open in new window

0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You didn't use classname but class, which does not exist. It works if you leave that check out, or use the correct property:
$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like "*Daily Employee Overtime*" }  | select -expand href
<# or #>
$link = $ie.Document.getElementsByTagName("a") | ? { $_.ClassName -eq "newReport -and $_.InnerText -like "*Daily Employee Overtime*" }  | select -expand href

Open in new window

You should always check with a format-table -auto or the like, as with
$ie.Document.getElementsByTagName("a") | ft -a classname, tagname, innerText

Open in new window

to see how many hits you get, and what makes them different from each other.
0
Leo TorresSQL DeveloperAuthor Commented:
My apologies in my code I dont see a Classname tag. I thought you did a Typo. I am still not getting a result for code:

$ie.Document.getElementsByTagName("a") | ft -a classname, tagname, innerText

Open in new window


All the code is listed below see if your code still works when you look for it in the entire page. It did not work for me. neither code you wrote.
0
Leo TorresSQL DeveloperAuthor Commented:
Forgot attachment.
USAmpleHTML.txt
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
With the attachment I get this output from the test code in http:#a40250814
className tagName innerText
--------- ------- ---------
          A        Reports
          A        Cross-Industry Reports
          A        Premium Reports
          A        Attendance Report
          A        Balance Report
          A        Exception Report
          A        Shift Pattern Report
          A        User Login Report
          A        Year at a Glance
newReport A       Adjustment Hours and Dollars Report
newReport A       Daily Employee Overtime Detail Report
newReport A       Payroll Control Totals
newReport A       Audit Tracking Report
newReport A       Daily Employee Overtime Report
newReport A       Daily Error and Exceptions Report
newReport A       Daily Project Activity and Docket Report
newReport A       Daily Time and Attendance Report
newReport A       Detailed Time Code by Employee Report
newReport A       Employee Count Report
newReport A       Employee Information Report
newReport A       Employee Overtime Report
newReport A       Employee Punches By Day Report
newReport A       Employee Seniority Report
newReport A       Export Log Report
newReport A       Forecast Wage Cost Report
newReport A       Hours by Time Code Report
newReport A       Import Log Report
newReport A       Job Scheduler Report
newReport A       Net Absenteeism Report
newReport A       Overtime Report
newReport A       Payroll Export Audit Report
newReport A       Payroll Export Audit Report - Retros
newReport A       Retro Adjustment Report
newReport A       Scheduled Reports
newReport A       Shift Pattern Detail Report
newReport A       Time Code by Employee Report
newReport A       Time Code by Team Report
newReport A       Unauthorized Records Report
newReport A       User Session Report
newReport A       Work In/Out Report
newReport A       Work Summary Report

Open in new window

Since your -like would result in two hrefs, we have to use a more restrictive condition:
$link = $ie.Document.getElementsByTagName("a") | ? { $_.ClassName -eq "newReport" -and $_.InnerText -eq "Daily Employee Overtime Report" }  | select -expand  href

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leo TorresSQL DeveloperAuthor Commented:
Then it may have something to do with how I get to page


CLS

$url = "https://psecu.ultiprotime.com"
$ie = New-Object -comobject InternetExplorer.Application
$ie.visible = $true
$ie.silent = $true
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1}

$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'loginField' } | % { $_.value = "user" }
$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'passwordField' } | % { $_.value = "pass" }
$ie.Document.getElementsByTagName("button") | ? { $_.Type -eq 'button' } | % { $_.Click() } 


$ReportPart = "/interface/folderTree.jsp?rootId=216&expandLevel=1&clearUIPath=true&uiPathLabel=Reports"
$ReportLink = $url + $ReportPart

$ie.navigate2($ReportLink)

$ie.Document.getElementsByTagName("a") | ft -a classname, tagname, innerText

Open in new window


in this context I get nothing but the above code do navigate to page.
0
Leo TorresSQL DeveloperAuthor Commented:
this is what I get when I run Script

System.__ComObject
0
Leo TorresSQL DeveloperAuthor Commented:
I guess my question is then how did you get the data into $ie variable?

because code
$ie.Document.getElementsByTagName("a") | ft -a Id, type, innerText

Open in new window

is not returning any thing.

Ok basically this is what my code is doing.
1. Open webpage -->(Currently working)
2. Login to webpage -->(Currently working)
3. click on Report Link -->(Currently working)
4. Click the Daily Employee Overtime Report (where I am stuck now I need to get link for report in href)
5. Once on report page Click go --> {Next still not working}
6. verify that report ran successfully else take note and record link to table variable {still not working}
0
Leo TorresSQL DeveloperAuthor Commented:
Ok I am getting the same table from above.

I know what happen looks like the code was executing to fast and the values are empty when it ran.
I added
Start-Sleep -s 5

Open in new window


Before select and the select returned values the link = expression does not
I even went to 10 Seconds and still no values.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The delay in retrieving the new page explains much. I had to work with a HTML file, of course, so no delay here.
For testing you can pause the script with e.g. read-host before reading the link. Since your IE is set up to be visible, you can manually trigger the second part. That way you can make sure the length of the delay is not the issue. And currently it looks like you are filtering the wrong way, as the "select returned values the link = expression does not". I reckon you mean the format-table, not select, here - if so, have a close look at the InnerText, which has to match obviously to get the link.
As said, what I have shown is working with the exact result page you supplied. There has to be a small difference, though.
0
Leo TorresSQL DeveloperAuthor Commented:
Yes this was the answer I just need to add a sleep in order for the data to become available.

Need help with this question now should not be to hard

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28494276.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.