GetElementsByClass help please

Yes i’m aware, and I see examples where the property is there, but the original error that was thrown suggests the property isn’t there when that run was done. This might be because of some other factor but felt it important enough to ‘double check’; I’m trying to confirm that whatever process is running this script (or maybe it is just ad-hoc) is using PowerShell 5.1, not just the ad-hoc examples provided. For example, perhaps this script is configured as a Scheduled Task and it’s actually calling PS Core, not 5, but when OP is testing, he’s launching in PS5. I think it’s worth a confirming that with OP just to make sure the environment isn’t changing unintentionally. I can’t tell you how many times I’ve had to help co-workers and they were simply using the wron version of PS when they weren’t aware. When it comes down to brass tacks, it works in 5.1 so if OP is truly using 5.1, there’s something else at play there. Either OP was blocked, or something funky on the system, so the next steps would be try a different computer on a different network.

Agreed, that’s what makes the most sense.

Thanks for all your help on this one…

To answer some of the questions -
Pretty sure it is 5.1

PS C:\Users\User> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      19041  4046    

I only have one windows machine that I can test with but I have switched to another wifi and tried the script with same results (see below). No Task Scheduling is involved, I just copied the code from this site and tried it with a different website and element name. I’m not sure about the blocking issue as it has never worked from the start and I am still able to get the text I am after by using the AppleScript code I posted earlier.

For centuries church bells have sounded.........

PS C:\Users\User> C:\Users\User\Desktop\TEST-elemementsByClass.ps1

You cannot call a method on a null-valued expression.
At C:\Users\User\Desktop\TEST-elemementsByClass.ps1:12 char:5
+     $news = $geturl.ParsedHtml.body.getElementsByClassName("elementor ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Cannot index into a null array.
At C:\Users\User\Desktop\TEST-elemementsByClass.ps1:13 char:19
+     Write-Host "$($news[0].outerText)"
+                   ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

This one works fine…

 function check-Victory {
  $geturl = Invoke-WebRequest -Uri "https://www.nmrn.org.uk/visit-us/portsmouth-historic-dockyard/hms-victory/"
        $news = $geturl.ParsedHtml.body.getElementsByClassName("exhibition-page__intro")
    Write-Host "$($news[0].outerText)"
    }

check-Victory

Gets me -

Step aboard one of the most celebrated warships in Britain’s naval history, HMS Victory.
Open daily from 10am

Can you reply to this post please.

I agree that you don’t appear to be blocked as the response size is the same. It’s looking like a parsing problem, I think.

Hi Matt, I thought I had replied. Next post down - is that not the info you were expecting?

Sorry, missed that reply completely.

Well, the plot thickens as that’s the same version I have and the same PS version too.

Does it have any body properties at all?

$geturl.ParsedHtml | Get-Member *body*

Might also be worth opening Internet Explorer and flushing all cache, cookies, and history. I’ve no idea if that affects the parsing but I’m clutching at straws now as everything from your program version to the responses you’re getting look the same as mine.

I think I’ve cleared everything out but am getting the same result


PS C:\Users\User>  $geturl | Get-Member

   TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject

Name              MemberType Definition                                                                 
----              ---------- ----------                                                                 
Dispose           Method     void Dispose(), void IDisposable.Dispose()                                 
Equals            Method     bool Equals(System.Object obj)                                             
GetHashCode       Method     int GetHashCode()                                                          
GetType           Method     type GetType()                                                             
ToString          Method     string ToString()                                                          
AllElements       Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection AllElements {get;}
BaseResponse      Property   System.Net.WebResponse BaseResponse {get;set;}                             
Content           Property   string Content {get;}                                                      
Forms             Property   Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;}            
Headers           Property   System.Collections.Generic.Dictionary[string,string] Headers {get;}        
Images            Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}     
InputFields       Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;}
Links             Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;}      
ParsedHtml        Property   mshtml.IHTMLDocument2 ParsedHtml {get;}                                    
RawContent        Property   string RawContent {get;set;}                                               
RawContentLength  Property   long RawContentLength {get;}                                               
RawContentStream  Property   System.IO.MemoryStream RawContentStream {get;}                             
Scripts           Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;}    
StatusCode        Property   int StatusCode {get;}                                                      
StatusDescription Property   string StatusDescription {get;}                                            

PS C:\Users\User> $geturl.ParsedHtml | Get-Member *Body*

PS C:\Users\User>

Please post the full output of $PSVersionTable.

PS C:\Users\User> $psversiontable

Name                           Value                                                                                                                           
----                           -----                                                                                                                           
PSVersion                      5.1.19041.4046                                                                                                                  
PSEdition                      Desktop                                                                                                                         
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                         
BuildVersion                   10.0.19041.4046                                                                                                                 
CLRVersion                     4.0.30319.42000                                                                                                                 
WSManStackVersion              3.0                                                                                                                             
PSRemotingProtocolVersion      2.3                                                                                                                             
SerializationVersion           1.1.0.1        

Off out now but back later…

OK, that’s the same as mine. Just wanted to rule out .NET version as I found a Reddit post suggesting that could be the problem.