VB.Net - 使用 GetElementByClass 选择一个类,然后以编程方式单击该项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15933704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:12:39  来源:igfitidea点击:

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

htmlvb.netwebbrowser-control

提问by eqiz

Now before posting this question i did read...

现在在发布这个问题之前,我确实阅读了...

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

如何通过 GetElementByClass 选择一个类并以编程方式单击它

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...

但这对我不起作用。显然我是个白痴,不知道如何使用该代码,或者它对我不起作用。如果这有帮助的话,我也在 VB.NET 中使用 WebBrowser 控件...

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

现在当然这被埋在大量其他 div 的下方和内部,但它没有任何直接的“所有者”。没有或类似的东西。它只是一个人坐着

Here is the html its barried in

这是它被禁止的 html

<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.

现在我只想能够挑出然后仅在此 div 和此 div 上调用“单击”。我知道什么时候因为服务器上的一些 PHP 代码而被点击,但这需要很长时间才能解释它是如何工作的。

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 ...

任何人都知道我怎么能把它挑出来,所以当我在 .NET 中有一个变量 XITEM 或其他东西时,我可以调用它?就像如果我有...

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

然后它只点击“选择”的 HTMLELEMENT。希望这对每个人都有意义

回答by eqiz

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

在把我在 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