eqiz
eqiz

Reputation: 1591

VB.Net - select a class using GetElementByClass and then click the item programmatically

Now before posting this question i did read...

How to select a class by GetElementByClass and click on it programmically

But this doesn't work for me. Apparently I'm an idiot and don't know how to use that code, or its just not working for me. I also am using the WebBrowser Control in VB.NET if that helps at all...

I have the following I'm trying to click...

<div class="closewindow"></div>

Now of course this is buried below and inside tons and tons of other divs, but it doesn't have any direct "owner". There is no or anything like that. Its just sitting by itself

Here is the html its barried in

<div class="main_class">  
<div id="FullItemView">  
<div style="width: 90%; float: left;">  
<div class="headline">  
<table style="width: 100%;">    
<tbody><tr>      
<td style="width: 15%; text-align: right; vertical-align: middle;">
<img class="switchImage" src="pictures/pic.png"></td>      
<td style="width: 70%; vertical-align: middle;">
<span class="ListItemTitle">Renegade</span></td>      
<td style="width: 15%; text-align: left;">
<img class="switchImage" src="pictures/pic.png"></td>    </tr>  </tbody></table>  
</div>  
</div>  
<div class="closeWindow"></div>
......

Now I just want to be able to single out the and then invoke a "click" on this div and this div only. I know when its being clicked because of some PHP code thats on the server, but that would take too long to explain how that works.

Anyone know how i can single this out so when i have a variable XITEM or something inside .NET then i can invoke it? Like if I had ...

For XITEM as HTMLELEMENT in WebBrowser1.Document.GetElementsByTagName("div")
......
item.invokeMember("click")
.....

Then it clicks only the HTMLELEMENT "chosen". Hopefully this makes sense to everyone

Upvotes: 0

Views: 19720

Answers (1)

eqiz
eqiz

Reputation: 1591

I figured it out after messin around with the original post that I found on stackoverflow.com

Dim theElementCollection As HtmlElementCollection = Nothing
        theElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
        For Each curElement As HtmlElement In theElementCollection
            'If curElement.GetAttribute("classname").ToString = "example"  It doesn't work.  
            ' This should be the work around.
            If InStr(curElement.GetAttribute("classname").ToString, "closeWindow") Then
                ' Doesn't even fire.
                ' InvokeMember(test) after class is found.
                'MessageBox.Show(curElement.GetAttribute("InnerText"))
                curElement.InvokeMember("Click")
                curElement.InvokeMember("MouseDown")
                curElement.InvokeMember("MouseUp")
                curElement.RaiseEvent("OnClick")
                curElement.Focus()
            End If
        Next

Upvotes: 2

Related Questions