Wei Wei
Wei Wei

Reputation: 99

Powershell IE object finding correct element

I am trying to access an element of a popup window, which seems to be embedded in an iframe. Below is the html I am dealing with. The line I am interested in below is ...OK.../a> . I have left some html out as it is rather large and I did not think it would be helpful. If needed I can post. I have tried many combinations to access this but so far nothing gets that OK button. Below is my best guess code that does not work.

$ie.Document.getElementsByTagName("A") | Where {$_.getAttributeNode 
('class').Value -eq 'btn btn3d PopupBtn'} 

<html>
<head>...</head>
  <body>
    <iframe id="12345"src="https://example.com/html/messagepopup.html"  
     frameborder="0" style="width:100%; height: 154px;">
     <!-- COMMENT HERE about the software
     -->
     <html>
     <head>
     <link href="https://example.com/path/to/custompage.css"rel=stylesheet">
     </link>
     </head>
     <body class="popupmsg" onload="onload()">
     <div class="PopupMsgFooter" id="PopupMsgFooter">
     <a class="btn bnt3d PopupBtn" href="#" arid="2"arwindowid="0">OK</a>
     </body>
     </html>
     </iframe>   
 </body>
</html> 

Upvotes: 1

Views: 4464

Answers (2)

Wei Wei
Wei Wei

Reputation: 99

Here is the final answer. Very close to the below but my final answer is convoluted. I had two iframes in this HTML DOC and for some reason searching by src would not return the correct result. Instead I had to eliminate the other. Weird but works. Second line is a hodge podge of stuff. I googled and found that trying contentwindow may work. After a number of iterations, I came up with the below line. Instead of contentdocument, I had to use contentwindow. (Anyone know why??) I also needed to change the where clause, due to there being two buttons under the same classname. The unique value for me here was the arid. Once I got a reference to the correct object it was easy to see the needed changes. Serious KUDOS to wOxxOm for this.....

$iframe =  $ie.Document.getElementsByTagName('iframe') 
    | Where { $_.id -notcontains 'pinghtml' }

$ok = $iframe.contentwindow.document.body.getElementsByTagName('a') 
    | Where {$_.getAttributeNode('arid').Value -eq '2'}

$ok.click()

Upvotes: 1

woxxom
woxxom

Reputation: 73856

The button is inside an iframe, and an iframe has its own separate DOM.

  1. Find the iframe element
  2. Find the button inside the iframe's contentDocument

$iframe = $ie.Document.getElementsByTagName('iframe') | 
    Where { $_.src -contains '/messagepopup.html' }
$ok = $iframe.contentDocument.getElementsByTagName('a') |
    Where { $_.className -eq 'btn bnt3d PopupBtn' }

Upvotes: 0

Related Questions