Skip to content Skip to sidebar Skip to footer

Powershell / Html - Questions

I am new to Powershell, and I suck at html. There's a page with a table, and each cell has a ahref link, the value of the link is dynamic, but the link which I want to automate-cli

Solution 1:

The result of Invoke-WebRequest returns a property named Links that is a collection of all the hyperlinks on a web page.

For example:

$Web = Invoke-webrequest -Uri 'http://wragg.io'$Web.Links | Select innertext,href

Returns:

innerTexthref-------------MarkWragghttp://wragg.ioTwitterhttps://twitter.com/markwraggGithubhttps://github.com/markwragg LinkedInhttps://uk.linkedin.com/in/mwragg

If the link you want to capture is always the first in this list you could get it by doing:

$Web.Links[0].href

If it's the second [1], third [2] etc. etc.

I don't think there is an equivalent of "cellindex", although there is a property named AllElements that you can access via an array index. E.g if you wanted the second element on the page you could for example do:

$Web.AllElements[2]

If you need to get to a specific table in the page and then access links inside of that table you'd probably need to iterate through the AllElements property until you reached the table you wanted. For example if you know the links were in the third table on the page:

$Links = @()
$TableCount = 0$Web.AllElements | ForEach-Object {

    If ($_.tagname -eq 'table'){ $TableCount++ }

    If ($TableCount -eq 3){

        If ($_.tagname -eq 'a') {
            $Links += $_
        }
    }
}

$Links | Select -First 1

Post a Comment for "Powershell / Html - Questions"