Leo Torres
asked on
Powershell get href from page and assign to variable
In my very restricted test case this code was sufficient:
Do you get anything back? Or what is the issue with your code?
$ie.Document.getElementsByTagName("a") | ? { $_.Classname -eq "newreport" } | select -expand href
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?
ASKER
No did not work I want put the href string in a variable so I can use it
Here is some sample code:
from this string I need to assign this value below to a variable
This is the code I tried
Here is some sample code:
<span class=spacer> </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&report_name=DAILY+EMPLOYEE+OVERTIME+DETAIL+REPORT&mfrm_id=10613&uiPathLabel=Daily%20Employee%20Overtime%20Detail%20Report&mfrmUIPathLabel=Daily+Employee+Overtime+Detail+Report&currentID=10613&parentID=216">Daily Employee Overtime Detail Report</a><br>
<span class=spacer> </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&report_name=PAYROLL+CONTROL+TOTALS&mfrm_id=10623&uiPathLabel=Payroll%20Control%20Totals&mfrmUIPathLabel=Payroll+Control+Totals&currentID=10623&parentID=216">Payroll Control Totals</a><br>
<span class=spacer> </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&report_name=AUDIT+TRACKING+REPORT&mfrm_id=1080&uiPathLabel=Audit%20Tracking%20Report&mfrmUIPathLabel=Audit+Tracking+Report&currentID=1080&parentID=216">Audit Tracking Report</a><br>
from this string I need to assign this value below to a variable
/reports/cognos/reportParams.jsp?mfrm_id=10613&report_name=DAILY+EMPLOYEE+OVERTIME+DETAIL+REPORT&mfrm_id=10613&uiPathLabel=Daily%20Employee%20Overtime%20Detail%20Report&mfrmUIPathLabel=Daily+Employee+Overtime+Detail+Report&currentID=10613&parentID=216
This is the code I tried
$Link = $ie.Document.getElementsByTagName("a") | ? { $_.class -eq "newreport" -and $_.innerText -like '*Overtime Report*' } | select -expand href
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
You should always check with a format-table -auto or the like, as with$ie.Document.getElementsByTagName("a") | ft -a classname, tagname, innerText
to see how many hits you get, and what makes them different from each other.
ASKER
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:
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.
$ie.Document.getElementsByTagName("a") | ft -a classname, tagname, innerText
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.
ASKER
Forgot attachment.
USAmpleHTML.txt
USAmpleHTML.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Then it may have something to do with how I get to page
in this context I get nothing but the above code do navigate 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
in this context I get nothing but the above code do navigate to page.
ASKER
this is what I get when I run Script
System.__ComObject
System.__ComObject
ASKER
I guess my question is then how did you get the data into $ie variable?
because code
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}
because code
$ie.Document.getElementsByTagName("a") | ft -a Id, type, innerText
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}
ASKER
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
Before select and the select returned values the link = expression does not
I even went to 10 Seconds and still no values.
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
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.
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.
ASKER
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
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
ASKER