Home Dashboard Directory Help
Search

getElementsByClassname method always returns null by Graham Gold


Status: 

Active


2
0
Sign in
to vote
Type: Bug
ID: 794625
Opened: 7/20/2013 6:03:30 AM
Access Restriction: Public
2
Workaround(s)
view
2
User(s) can reproduce this bug

Description


Using invoke-WebRequest, attempting to use the following code to return all elements with the class name "newstitle", with the following code $news always returns nothing:

    $geturl=Invoke-WebRequest http://krpano.com/news/
    $news=$geturl.parsedhtml.body.GetElementsByClassName("newstitle")[0]
    Write-Host "$news"
Details
Sign in to post a comment.
Sign in to post a workaround.
Posted by Don Cruickshank on 3/1/2014 at 7:28 AM
You can use the list evaluation syntax to convert the results to an array first to access the individual elements:

    $geturl=Invoke-WebRequest http://krpano.com/news/
    $news=@($geturl.parsedhtml.body.GetElementsByClassName("newstitle"))[0]
    Write-Host "$news"
Posted by Graham Gold on 7/20/2013 at 6:08 AM
Depends upon the content required, either use getElementsByTagName then pipe through where-object looking for matches on the Class attribute, or inspect other parts of the IHTML object such as the Links collection.