3

I writing on a script that reads from a local HTML file, modifies DOM tree and then save. Instead of Invoke-WebRequest, using HTMLFile COM object seems the only way to do so.

Rewriting to other attributes like href works like a charm. But somehow I found it impossible to change an anchor's name attribute, unless recreating the whole element by setting outerHTML attribute which is complicated and ugly. However, removeAttribute method works but that's not what I want.

Test case:

$idoc = New-Object -ComObject "HTMLFile"
$idoc.IHTMLDocument2_write("<body><a name=123></a><a name=456></a></body>")
$idoc.anchors | foreach {$_.name = "aaa"; /* or $_.setAttribute("name", "aaa") */}
# $idoc.anchors | foreach {$_.getAttribute("name")} <- changed
# $idoc.anchors | foreach {$_.outerHTML} <- remains unchanged

Can any one tell me is this a bug or is there just any restriction on accessing the attribute? How can I found an elegant way to do my job?

Thanks.

0 Answers0