CactusCake
CactusCake

Reputation: 990

Powershell - click on a href link within div

I'm sure this must have been answered before but I guess I just don't know what I should be using in my search terminology.

I'm trying to get powershell to click on a simple 'a href' link. But all the powershell examples I've found for clicking on things so far are using buttons. when I try to rewrite the button-clicking scripts to click on the link instead I'm getting a "You cannot call a method on a null-valued expression" error message.

The source for the link I want to click is:

    <div id="reportTreer1r3" style="padding-left:32px;" uid="2.-83016" nowrap 
    class="treeNode" onClick="reportTree.onSelect('r1r3');" onDblClick="reportTree.onDblSelect('r1r3');
    " BP_REPORT_ID="-83016" reportType="report" isRemovable="false">
    <nobr>
      <a href="#r1r3" onClick="return false;" onKeyDown="reportTree.onKeyDown(this);">
        <span>
          <label class="hidden-label"> Tree level 1, 4 of 4. </label>
          <span>Adherence Summary</span>
        </span>
        <label name="SelectInfo" class="hidden-label"></label>
      </a>
    </nobr>
    </div>

Unfortunately the link hasn't been given an element id of its own so I can't just "getElementByID" on it, instead I've tried a bunch of things along the lines of...

    $link = $doc.getElementsByTagName('a') | Where-Object {$_.href -eq '#r1r3'} 
    $link.click() 

... but none of it seems to be working.

Any suggestions would be much appreciated!

Upvotes: 2

Views: 10849

Answers (2)

Jean Paul
Jean Paul

Reputation: 1

If you link (href) is inside on frame, you would use:

while($ie.busy) {sleep 5} 
$dmeContExpFrame = $ie.Document.getElementById("portfolio_frameset") 
$dmecontexpframedoc = $dmeContExpFrame.contentWindow 
$dmeContExpFramedoc2 = $dmecontexpframedoc.Document 
$click1 = $dmeContExpFramedoc2.getElementsByTagName("a") | ?{$_.href -match "system_status.ssi"}    
$click1.click()

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36297

I think the issue here isn't entirely your code, it's what you're using to select the specific link. You'll need to escape the # with a grave character `. Also you want to use -match instead of -eq since $_.href contains more than just #r1r3, it just ends in that.

$link = $doc.getElementsByTagName('a') | Where-Object {$_.href -match '`#r1r3'} 
$link.click() 

That should work for you.

Edit: Here's what I did. I saved your sample HTML in a document test.html on my desktop, because I'm lazy like that. Then I did:

$ie = new-object -ComObject InternetExplorer.Application
$ie.Navigate("C:\users\310080829\Desktop\test.html")
$IE.Document.getElementsByTagName('a')

Now that spits back a ton of stuff, so you'll have to pardon me if I don't paste it all in here. But then I tried the Where bit:

$IE.Document.getElementsByTagName('a')|?{$_.href -match '`#r1r3'}

...and it returned nothing. Um... WTF. Oh, wait, I used single quotes not double quotes. Oops, try again with -match "`#r1r3" and things go much better.

$IE.Document.getElementsByTagName('a')|?{$_.href -match "`#r1r3"}

That's the ticket! Now I have an object that I can work with. Yeah, sure, some of the properties are True or False, but there's a bunch of other things like InnerHTML, InnerText, OuterHTML, OuterText... well, a lot. Plus if I pipe that to |GM it lists all the properties, and methods for the object... including Click.

So, I think the real issue is that I copied and pasted your code and just modified it a little, I didn't actually notice the ' vs " issue. Try again with:

$link = $doc.getElementsByTagName('a') | Where-Object {$_.href -match "`#r1r3"}
$Link.Click

Lesson learned on my part, sorry for the confusion.

Upvotes: 2

Related Questions