在C#中使用Web浏览器单击HTML元素 [英] Click an HTML Element with web browser in C#

查看:162
本文介绍了在C#中使用Web浏览器单击HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这里有一个代码,其中包含一个我需要点击的按钮,但我不能这样做,虽然我尝试了很多方法来使用

 currentwb.Document.GetElementById 



但是我们看到这个代码没有该元素的id,

< a  class  =   single_like_button btn3-wrapönclick=   openWin_1083724(); >  
< span> < / span > < div class = btn3>点击此处< / div > < / a >





所以有关如何获取此元素的任何建议被点击?

解决方案

你不能点击它?这对我来说毫无意义。你想从javascript触发元素上的click事件?试试这个: http://www.nonobtrusive.com/ 2011/11/29 / programatically-fire-crossbrowser-click-event-with-javascript / [ ^ ]



[更新]

如果您想自动化IE,最好使用这种方法: http://harness.codeplex.com/ [ ^ ]



如果您坚持使用WebBrowser控件(currentwb),请尝试以下方法:



 HtmlElement link = currentwb.Document.GetElementByID(  id_of_element
link.InvokeMember( 点击





或者,如果由于某种原因你不能指定id,你可以使用这种方法。

 HtmlElementCollection links = currentwb.Document.GetElementsByTagName(  A); 

foreach (HtmlElement link in links)
{
if (link.InnerText.Equals( Link text))
link.InvokeMember( 点击);
}



请注意在IF语句中输入识别链接的正确条件。


点击是你用鼠标或事实上用键盘做的事情。如果你仔细想想,你会明白你所需要的不是点击。你只想拥有与用户点击元素相同的效果。在其他作品中,您只需从JavaScript中调用 openWin_1083724()



-SA

hello guys , here a code that`s contains a button i need to click but i can`t do it although i tried many way to do it using

currentwb.Document.GetElementById


but as we see this code have no id for the element ,,

<a class="single_like_button btn3-wrap"  önclick="openWin_1083724();">
						                    <span> </span><div class="btn3">Click Here</div></a>



so any suggestion about how i can get this element is clicked ?

解决方案

You can't click on it? This makes no sense to me. You want to trigger the click event on the element from javascript? Try this: http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/[^]

[Update]
If you want to automate IE, you better use this approach: http://harness.codeplex.com/[^]

If you stick to WebBrowser control (currentwb), try this approach:

HtmlElement link = currentwb.Document.GetElementByID("id_of_element")
link.InvokeMember("Click")



Or, if you can't assign id for some reason, you can use this approach.

HtmlElementCollection links = currentwb.Document.GetElementsByTagName("A");

foreach (HtmlElement link in links)
{
    if (link.InnerText.Equals("Link text"))
        link.InvokeMember("Click");
}


Be aware to put in the IF statement the proper condition to identify your link.


"Click" is something which you do with a mouse or, in fact, with a keyboard. If you think about it, you will understand that what you need is not a click. You simply want to have "the same effect as the user's click on the element". In other works, you simply need to call openWin_1083724() from your JavaScript.

—SA


这篇关于在C#中使用Web浏览器单击HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆