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

血红的双手。 提交于 2020-01-16 19:06:48

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/15933704/vb-net-select-a-class-using-getelementbyclass-and-then-click-the-item-programm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!