Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

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
User generated image
Avatar of Leo Torres
Leo Torres
Flag of United States of America image

ASKER

If you need more HTML let me know I will share i posting what I think is most relevant.
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?
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

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.
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.
Forgot attachment.
USAmpleHTML.txt
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
this is what I get when I run Script

System.__ComObject
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}
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.
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.
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

https://www.experts-exchange.com/questions/28494276/Powershell-need-to-find-this-value.html