开发者

How do I programmatically click on a div in a web browser?

开发者 https://www.devze.com 2023-02-09 03:21 出处:网络
I have been trying for a few days now and still have had no success in programmatically clicking on this div.All of the other input fields and buttons are working fine using InvokeMember(\"click\") an

I have been trying for a few days now and still have had no success in programmatically clicking on this div. All of the other input fields and buttons are working fine using InvokeMember("click") and RaiseEvent("onclick"), but I am unable to click on the following div:

<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>

This div is repeated several times on a page, but I just want to click on the first occurrence.

This is what I have done so far:

 HtmlElementCollection c1 = wbc开发者_Python百科1.document.GetElementsByTagName("div");

 foreach (HtmlElement e2 in c1)
 {
    if (e2.GetAttribute("class").Contains("pump request"))//also this condition is not returning true
       {
          e2.RaiseEvent("onclick");              
       }
 }

@bleepzter

what if "somecontrol" is a class of the div instead of div's id?

since in my case i have div class "pump request" so (if i write "pump request" as somecontrol in above example) it return me Null in someDiv

<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>

@Cameron

yep i did entered the break; but the problem is the if condition never returns true so

    HtmlElementCollection c1 = wbc1.document.GetElementsByTagName("div");
    foreach (HtmlElement e2 in c1)
 {
    if (e2.GetAttribute("class").Contains("pump request"))//--> This condition is not returning true
       {
          e2.RaiseEvent("onclick"); 
          break;              
       }
 }

@Ilya Kogan

yea i just did a watch on e2.GetAttribute("class") and the weird thing happened that being reading the actual div (which i want to click) the value of class was empty :-o


try this one

if (e2.GetAttribute("className").Contains("pump request"))
{
    e2.InvokeMember("Click");              
}


you can try this piece of code by using a web browser control.

 // browser is the web browser control
 HtmlElementCollection col = browser.Document.GetElementsByTagName("div");
            foreach (HtmlElement helemnt in col)
            {
                if (helemnt.InnerText !=null && helemnt.InnerText=="something") 
                {

                    helemnt.InvokeMember("Click");

                       break; // break the loop

                }



            }


It is simple. Here is an example which assumes your browser control is called browser, and the div you are looking for is called somecontrol (i.e. the id of the div is somecontrol):

HtmlElement someDiv = browser.Document.All["somecontrol"];
object someDivElement = someDiv.DomElement;

MethodInfo clickMethod = someDivElement.GetType().GetMethod("click");
clickMethod.Invoke(someDivElement, null);

All this is possible via reflection.

0

精彩评论

暂无评论...
验证码 换一张
取 消