Search code examples
importgetelementbyidgetelementsbytagnamegetelementsbyclassname

PowerShell Cannot get element from HTML


I encountered a problem today while trying to get a value from a website using PowerShell.

This is the website.

I am trying to get the number "90" here, that in the webpage itself is the value of "Downloaded" (this number might be a little bigger when you look at it if there are more downloads):Screenshot of the element i am trying to return

<span title="Downloads" class="mod-card-info-tag" data-reactid=".0.0.0.2.0.0.2.2">
<div class="mod-card-info-tag-label" data-reactid=".0.0.0.2.0.0.2.2.0">90</div>

This is the PowerShell code i used to try and get the number "90" from the element above (i know i should use ".innertext" in the end, i just used the get-member to see if any object was found):

$URI = "https://mods.factorio.com/mods/TpTheGreat/TpTheGreats%20Large%20Roboport%20Logistics%20Area"
$HTML = Invoke-WebRequest  -Uri $URI

($HTML.ParsedHtml.getElementsByTagName("div") | Where{ $_.className -eq ‘mod-card-info-tag-label’ }) | Get-Member

When calling for the element by tag name like in my code above, I get an empty object. I tried lots of things without any success.

It'll be really great if any of you could take a look and check if you are able to solve my problem. Thanks a lot!!!


Solution

  • How about another approach:

     $URI = "https://mods.factorio.com/mods/TpTheGreat/TpTheGreats%20Large%20Roboport%20Logistics%20Area"
     $HTML = Invoke-WebRequest  -Uri $URI
     $arr = $HTML.AllElements.Item(9).innerHTML  -split ' = '
    
     $myObj = $arr[1].replace("`n"," ") 
    
     $myObj = $myObj.replace(";","") | ConvertFrom-Json
    
     $myObj.mod.mod.downloads_count